|
| 1 | +// Copyright 2024 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +const {fetch} = require('discovery-tsd'); |
| 16 | +const TypeGenerator = require('discovery-tsd/src/generator'); |
| 17 | +const prettier = require('prettier'); |
| 18 | +const fs = require('fs'); |
| 19 | +const {promisify} = require('util'); |
| 20 | + |
| 21 | +const writeFile = promisify(fs.writeFile); |
| 22 | + |
| 23 | +const LICENSE = `// Copyright 2024 Google LLC |
| 24 | +// |
| 25 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 26 | +// you may not use this file except in compliance with the License. |
| 27 | +// You may obtain a copy of the License at |
| 28 | +// |
| 29 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 30 | +// |
| 31 | +// Unless required by applicable law or agreed to in writing, software |
| 32 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 33 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 34 | +// See the License for the specific language governing permissions and |
| 35 | +// limitations under the License.`; |
| 36 | + |
| 37 | +function overridedRender() { |
| 38 | + const source = this.template({ |
| 39 | + title: this.title ? this.converter.toJSDoc(this.title) : '', |
| 40 | + name: this.name, |
| 41 | + schemas: this.schemas.map(schema => this.converter.createType(schema)), |
| 42 | + resources: this.resources.map(resource => resource.render()), |
| 43 | + }); |
| 44 | + |
| 45 | + const patched = source.replaceAll( |
| 46 | + 'formatOptions.useInt64Timestamp', |
| 47 | + "'formatOptions.useInt64Timestamp'" |
| 48 | + ); |
| 49 | + const sourceWithLicense = LICENSE + '\n' + patched; |
| 50 | + |
| 51 | + return prettier.format(sourceWithLicense, { |
| 52 | + parser: 'typescript', |
| 53 | + singleQuote: true, |
| 54 | + }); |
| 55 | +} |
| 56 | + |
| 57 | +async function genTypes() { |
| 58 | + const json = await fetch('bigquery', 'v2'); |
| 59 | + const generator = new TypeGenerator(json); |
| 60 | + generator.render = overridedRender.bind(generator); |
| 61 | + const types = await generator.render(); |
| 62 | + await writeFile('./src/types.d.ts', types); |
| 63 | +} |
| 64 | + |
| 65 | +genTypes(); |
0 commit comments