Skip to content

Typeid support #2034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/openapi-ts/src/ir/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ interface IRSchemaObject
| 'pattern'
| 'required'
| 'title'
| 'example'
> {
/**
* If the schema is intended to be used as an object property, it can be
Expand Down
4 changes: 4 additions & 0 deletions packages/openapi-ts/src/openApi/2.0.x/parser/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
irSchema: IR.SchemaObject;
schema: SchemaObject;
}) => {
if (schema.example) {
irSchema.example = schema.example;
}

Check warning on line 37 in packages/openapi-ts/src/openApi/2.0.x/parser/schema.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/openApi/2.0.x/parser/schema.ts#L35-L37

Added lines #L35 - L37 were not covered by tests

if (schema.description) {
irSchema.description = schema.description;
}
Expand Down
4 changes: 4 additions & 0 deletions packages/openapi-ts/src/openApi/3.0.x/parser/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
irSchema.deprecated = schema.deprecated;
}

if (schema.example) {
irSchema.example = schema.example;
}

Check warning on line 41 in packages/openapi-ts/src/openApi/3.0.x/parser/schema.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/openApi/3.0.x/parser/schema.ts#L39-L41

Added lines #L39 - L41 were not covered by tests

if (schema.description) {
irSchema.description = schema.description;
}
Expand Down
4 changes: 4 additions & 0 deletions packages/openapi-ts/src/openApi/3.1.x/parser/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
irSchema.deprecated = schema.deprecated;
}

if (schema.example) {
irSchema.example = schema.example;
}

Check warning on line 47 in packages/openapi-ts/src/openApi/3.1.x/parser/schema.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/openApi/3.1.x/parser/schema.ts#L45-L47

Added lines #L45 - L47 were not covered by tests

if (schema.description) {
irSchema.description = schema.description;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const defaultConfig: Plugin.Config<Config> = {
readableNameBuilder: '{{name}}Readable',
style: 'preserve',
tree: false,
typeids: false,
writableNameBuilder: '{{name}}Writable',
};

Expand Down
42 changes: 42 additions & 0 deletions packages/openapi-ts/src/plugins/@hey-api/typescript/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,10 +617,12 @@

const stringTypeToIdentifier = ({
context,
plugin,

Check warning on line 620 in packages/openapi-ts/src/plugins/@hey-api/typescript/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/@hey-api/typescript/plugin.ts#L620

Added line #L620 was not covered by tests
schema,
}: {
context: IR.Context;
namespace: Array<ts.Statement>;
plugin: Plugin.Instance<Config>;
schema: SchemaWithType<'string'>;
}): ts.TypeNode => {
if (schema.const !== undefined) {
Expand Down Expand Up @@ -649,6 +651,20 @@
return compiler.typeReferenceNode({ typeName: 'Date' });
}
}

if (schema.format === 'typeid' && schema.example && plugin.typeids) {
const type = String(schema.example).split('_')[0]!;
return compiler.typeReferenceNode({
typeArguments: [
compiler.literalTypeNode({
literal: compiler.stringLiteral({
text: type,
}),
}),
],
typeName: 'TypeID',
});
}

Check warning on line 667 in packages/openapi-ts/src/plugins/@hey-api/typescript/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/@hey-api/typescript/plugin.ts#L655-L667

Added lines #L655 - L667 were not covered by tests
}

return compiler.keywordTypeNode({
Expand Down Expand Up @@ -763,6 +779,7 @@
return stringTypeToIdentifier({
context,
namespace,
plugin,

Check warning on line 782 in packages/openapi-ts/src/plugins/@hey-api/typescript/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/@hey-api/typescript/plugin.ts#L782

Added line #L782 was not covered by tests
schema: schema as SchemaWithType<'string'>,
});
case 'tuple':
Expand Down Expand Up @@ -1255,6 +1272,31 @@
namespace: 'type',
});

if (plugin.typeids) {
const typeParameter = compiler.typeParameterDeclaration({
constraint: compiler.keywordTypeNode({
keyword: 'string',
}),
name: 'T',
});
const node = compiler.typeAliasDeclaration({
name: 'TypeID',
type: compiler.templateLiteralType({
value: [
compiler.typeReferenceNode({
typeName: 'T',
}),
'_',
compiler.keywordTypeNode({
keyword: 'string',
}),
],
}),
typeParameters: [typeParameter],
});
file.add(node);
}

Check warning on line 1298 in packages/openapi-ts/src/plugins/@hey-api/typescript/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/@hey-api/typescript/plugin.ts#L1275-L1298

Added lines #L1275 - L1298 were not covered by tests

context.subscribe('schema', ({ $ref, schema }) => {
if (
plugin.readOnlyWriteOnlyBehavior === 'off' ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@
* @default '{{name}}Writable'
*/
writableNameBuilder?: string;
/**
* Generate types for [TypeIDs](https://github.com/jetify-com/typeid/tree/main/spec).
*
* @default false
*/
typeids?: boolean;

Check warning on line 75 in packages/openapi-ts/src/plugins/@hey-api/typescript/types.d.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, Test (macos-latest, 22.11.0)

Expected interface keys to be in ascending order. 'typeids' should be before 'writableNameBuilder'

Check warning on line 75 in packages/openapi-ts/src/plugins/@hey-api/typescript/types.d.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, Test (macos-latest, 18.20.5)

Expected interface keys to be in ascending order. 'typeids' should be before 'writableNameBuilder'

Check warning on line 75 in packages/openapi-ts/src/plugins/@hey-api/typescript/types.d.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, Test (macos-latest, 20.11.1)

Expected interface keys to be in ascending order. 'typeids' should be before 'writableNameBuilder'

Check warning on line 75 in packages/openapi-ts/src/plugins/@hey-api/typescript/types.d.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, Test (ubuntu-latest, 18.20.5)

Expected interface keys to be in ascending order. 'typeids' should be before 'writableNameBuilder'

Check warning on line 75 in packages/openapi-ts/src/plugins/@hey-api/typescript/types.d.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, Test (ubuntu-latest, 22.11.0)

Expected interface keys to be in ascending order. 'typeids' should be before 'writableNameBuilder'

Check warning on line 75 in packages/openapi-ts/src/plugins/@hey-api/typescript/types.d.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, Test (windows-latest, 18.20.5)

Expected interface keys to be in ascending order. 'typeids' should be before 'writableNameBuilder'

Check warning on line 75 in packages/openapi-ts/src/plugins/@hey-api/typescript/types.d.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, Test (windows-latest, 20.11.1)

Expected interface keys to be in ascending order. 'typeids' should be before 'writableNameBuilder'

Check warning on line 75 in packages/openapi-ts/src/plugins/@hey-api/typescript/types.d.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, Test (ubuntu-latest, 20.11.1)

Expected interface keys to be in ascending order. 'typeids' should be before 'writableNameBuilder'

Check warning on line 75 in packages/openapi-ts/src/plugins/@hey-api/typescript/types.d.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, Test (windows-latest, 22.11.0)

Expected interface keys to be in ascending order. 'typeids' should be before 'writableNameBuilder'

// DEPRECATED OPTIONS BELOW

Expand Down