Skip to content

Commit

Permalink
fix(rest): Find a roundabout way to sync the upstream OAS
Browse files Browse the repository at this point in the history
  • Loading branch information
Sidnioulz committed Oct 19, 2024
1 parent 6250703 commit 7e5f152
Show file tree
Hide file tree
Showing 4 changed files with 759 additions and 417 deletions.
3 changes: 2 additions & 1 deletion packages/rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@
"tsc-watch": "^6.2.0",
"tsup": "^8.3.0",
"typescript": "latest",
"vitest": "^2.1.3"
"vitest": "^2.1.3",
"yaml": "^2.6.0"
},
"dependencies": {
"@figmarine/cache": "workspace:*",
Expand Down
38 changes: 35 additions & 3 deletions packages/rest/scripts/regenFigmaRestApi.mjs
Original file line number Diff line number Diff line change
@@ -1,17 +1,49 @@
import { createRequire } from 'node:module';
import https from 'node:https';
import path from 'node:path';

import { generateApi } from 'swagger-typescript-api';
import { parse } from 'yaml';

const require = createRequire(import.meta.url);
const spec = require('@figma/rest-api-spec/openapi.json');
import prettierConfig from '../prettier.config.js';

// import { createRequire } from 'node:module';
// const require = createRequire(import.meta.url);
// const spec = require('@figma/rest-api-spec/openapi.json');

// FIXME: Temporarily fetching the YAML spec directly from GitHub until
// https://github.com/figma/rest-api-spec/pull/18 is fixed upstream.
const OAS_URL = process.env.FIGMA_BRANCH
? `https://raw.githubusercontent.com/figma/rest-api-spec/refs/tags/${process.env.FIGMA_BRANCH}/openapi/openapi.yaml`
: 'https://raw.githubusercontent.com/figma/rest-api-spec/refs/heads/main/openapi/openapi.yaml';

function fetchUrl(url) {
return new Promise((resolve, reject) => {
https
.get(url, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
resolve(data);
});
})
.on('error', (err) => {
reject(err);
});
});
}

const yamlContent = await fetchUrl(OAS_URL);
const spec = parse(yamlContent);

await generateApi({
name: 'figmaRestApi.ts',
output: path.resolve(import.meta.dirname, '../src/__generated__'),
spec,
httpClientType: 'axios',
generateRouteTypes: true,
prettier: prettierConfig,
hooks: {
/** Hack used to make it possible to pass `cache: false` to request params. */
onCreateRequestParams: (data) => {
Expand Down
Loading

0 comments on commit 7e5f152

Please sign in to comment.