Skip to content

Commit

Permalink
fix: generate types for all resources
Browse files Browse the repository at this point in the history
  • Loading branch information
epeters3 committed Sep 21, 2023
1 parent 61d4c1b commit adaa208
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
106 changes: 106 additions & 0 deletions src/generate-api-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,112 @@ export const Schema: OneSchema<Endpoints> = {
},
},
};
`,
},
{
input: {
spec: {
Resources: {
Fruit: {
type: 'object',
additionalProperties: false,
required: ['name', 'price'],
properties: {
name: { type: 'string' },
price: { type: 'number' },
},
},
// This resource is not referenced by any endpoint, but should be
// included in the generated types.
Pet: {
type: 'object',
// type-level comments should be preserved.
description: 'A pet from the animal kingdom.',
additionalProperties: false,
required: ['name', 'age', 'species'],
properties: {
name: {
type: 'string',
// field-level comments should be preserved.
description: 'The name of the pet as given by the owner.',
},
age: { type: 'number' },
species: { type: 'string' },
},
},
},
Endpoints: {
'GET /fruits': {
Name: 'getPosts',
Request: {},
Response: {
type: 'array',
items: {
$ref: '#/definitions/Fruit',
},
},
},
},
},
},
expected: `/* eslint-disable */
import type { OneSchema } from "@lifeomic/one-schema";
export type Endpoints = {
"GET /fruits": {
Request: unknown;
PathParams: {};
Response: Fruit[];
};
};
export type Fruit = {
name: string;
price: number;
};
/**
* A pet from the animal kingdom.
*/
export type Pet = {
/**
* The name of the pet as given by the owner.
*/
name: string;
age: number;
species: string;
};
export const Schema: OneSchema<Endpoints> = {
Resources: {
Fruit: {
type: "object",
additionalProperties: false,
required: ["name", "price"],
properties: { name: { type: "string" }, price: { type: "number" } },
},
Pet: {
type: "object",
description: "A pet from the animal kingdom.",
additionalProperties: false,
required: ["name", "age", "species"],
properties: {
name: {
type: "string",
description: "The name of the pet as given by the owner.",
},
age: { type: "number" },
species: { type: "string" },
},
},
},
Endpoints: {
"GET /fruits": {
Name: "getPosts",
Request: {},
Response: { type: "array", items: { $ref: "#/definitions/Fruit" } },
},
},
};
`,
},
];
Expand Down
21 changes: 21 additions & 0 deletions src/generate-endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export const generateEndpointTypes = async ({
await compile(deepCopy(masterSchema), '', {
format: false,
bannerComment: '',
unreachableDefinitions: true,
})
)
/**
Expand All @@ -73,5 +74,25 @@ export const generateEndpointTypes = async ({
const name = matched.split(' ')[2];
return `export type ${name} = {`;
})
/**
* When the json-schema-to-typescript `unreachableDefinitions` setting is `true`, it outputs
* some ugly comments on the generated types, so we remove them.
*
* Relevant GitHub issue in json-schema-to-typescript:
* https://github.com/bcherny/json-schema-to-typescript/issues/428
*/
.replace(
/^\s*\/\*\*\s+\* This interface was referenced by `.*`'s JSON-Schema\s+\* via the `definition` ".*"\.\s+\*\//gm,
'',
)
/**
* Same as above ^^^ but handling the case where the ugly comment is appended to an already
* existing comment. We keep the original comment but remove json-schema-to-typescript's ugly
* addition.
*/
.replace(
/\n\s*\*\s+\* This interface was referenced by `.*`'s JSON-Schema\s+\* via the `definition` ".*"\./gm,
'',
)
);
};

0 comments on commit adaa208

Please sign in to comment.