Skip to content
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

chore(parser): pass openAPI schema to write methods #285

Merged
merged 4 commits into from
Apr 8, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/giant-sloths-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": minor
---

feat: export schemas directly from OpenAPI specification (ie. support exporting JSON schemas draft 2020-12
5 changes: 3 additions & 2 deletions packages/openapi-ts/src/compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export class TypeScriptFile {
}

public write(file: PathOrFileDescriptor, seperator: string = '\n') {
if (!this._items.length) {
return;
}
writeFileSync(file, this.toString(seperator));
}
}
Expand All @@ -45,5 +48,3 @@ export const compiler = {
object: types.createObjectType,
},
};

export default compiler;
3 changes: 3 additions & 0 deletions packages/openapi-ts/src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
Object.entries(obj)
.map(([key, value]) => {
const initializer = toExpression(value);
if (key.match(/\W/g) && !key.startsWith("'") && !key.endsWith("'")) {
key = `'${key}'`;
}

Check warning on line 62 in packages/openapi-ts/src/compiler/types.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/compiler/types.ts#L61-L62

Added lines #L61 - L62 were not covered by tests
return initializer ? ts.factory.createPropertyAssignment(key, initializer) : undefined;
})
.filter(isType<ts.PropertyAssignment>),
Expand Down
7 changes: 7 additions & 0 deletions packages/openapi-ts/src/compiler/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@
if (unescape) {
value = unescapeName(value);
}
if (
(value.includes('\n') || (value.includes("'") && value.includes('"'))) &&
!value.startsWith('`') &&
!value.endsWith('`')

Check warning on line 67 in packages/openapi-ts/src/compiler/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/compiler/utils.ts#L67

Added line #L67 was not covered by tests
) {
value = `\`${value}\``;
}

Check warning on line 70 in packages/openapi-ts/src/compiler/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/compiler/utils.ts#L69-L70

Added lines #L69 - L70 were not covered by tests
const text = encodeURIComponent(value);
if (value.startsWith('`')) {
return ts.factory.createIdentifier(text);
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@
if (config.write) {
logClientMessage(config.client);
logMissingDependenciesWarning(config.client, dependencies);
await writeClient(client, templates, config);
await writeClient(openApi, client, templates, config);

Check warning on line 201 in packages/openapi-ts/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/index.ts#L201

Added line #L201 was not covered by tests
processOutput(config, dependencies);
}

Expand Down
5 changes: 4 additions & 1 deletion packages/openapi-ts/src/openApi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ export { OpenApi } from './common/interfaces/OpenApi';
export function parse(openApi: OpenApi, config: Config): Client {
if ('openapi' in openApi) {
return parseV3(openApi, config);
} else if ('swagger' in openApi) {
}

if ('swagger' in openApi) {
return parseV2(openApi, config);
}

throw new Error(`Unsupported Open API specification: ${JSON.stringify(openApi, null, 2)}`);
}
51 changes: 29 additions & 22 deletions packages/openapi-ts/src/utils/write/__tests__/class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,47 @@ import { describe, expect, it, vi } from 'vitest';

import { writeClientClass } from '../class';
import { mockTemplates } from './mocks';
import { openApi } from './models';

vi.mock('node:fs');

describe('writeClientClass', () => {
it('should write to filesystem', async () => {
const client: Parameters<typeof writeClientClass>[0] = {
const client: Parameters<typeof writeClientClass>[2] = {
enumNames: [],
models: [],
server: 'http://localhost:8080',
services: [],
version: 'v1',
};

await writeClientClass(client, mockTemplates, './dist', {
client: 'fetch',
debug: false,
enums: 'javascript',
experimental: false,
exportCore: true,
exportModels: true,
exportSchemas: true,
exportServices: true,
format: false,
input: '',
lint: false,
name: 'AppClient',
operationId: true,
output: '',
postfixServices: '',
serviceResponse: 'body',
useDateType: false,
useOptions: true,
write: true,
});
await writeClientClass(
openApi,
'./dist',
client,
{
client: 'fetch',
debug: false,
enums: 'javascript',
experimental: false,
exportCore: true,
exportModels: true,
exportSchemas: true,
exportServices: true,
format: false,
input: '',
lint: false,
name: 'AppClient',
operationId: true,
output: '',
postfixServices: '',
serviceResponse: 'body',
useDateType: false,
useOptions: true,
write: true,
},
mockTemplates
);

expect(writeFileSync).toHaveBeenCalled();
});
Expand Down
5 changes: 3 additions & 2 deletions packages/openapi-ts/src/utils/write/__tests__/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ import { describe, expect, it, vi } from 'vitest';

import { writeClient } from '../client';
import { mockTemplates } from './mocks';
import { openApi } from './models';

vi.mock('node:fs');

describe('writeClient', () => {
it('should write to filesystem', async () => {
const client: Parameters<typeof writeClient>[0] = {
const client: Parameters<typeof writeClient>[1] = {
enumNames: [],
models: [],
server: 'http://localhost:8080',
services: [],
version: 'v1',
};

await writeClient(client, mockTemplates, {
await writeClient(openApi, client, mockTemplates, {
client: 'fetch',
debug: false,
enums: 'javascript',
Expand Down
15 changes: 8 additions & 7 deletions packages/openapi-ts/src/utils/write/__tests__/core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';

import { writeClientCore } from '../core';
import { mockTemplates } from './mocks';
import { openApi } from './models';

vi.mock('node:fs');

describe('writeClientCore', () => {
let templates: Parameters<typeof writeClientCore>[1];
let templates: Parameters<typeof writeClientCore>[4];
beforeEach(() => {
templates = mockTemplates;
});

it('should write to filesystem', async () => {
const client: Parameters<typeof writeClientCore>[0] = {
const client: Parameters<typeof writeClientCore>[2] = {
enumNames: [],
models: [],
server: 'http://localhost:8080',
Expand Down Expand Up @@ -45,7 +46,7 @@ describe('writeClientCore', () => {
write: true,
};

await writeClientCore(client, templates, '/', config);
await writeClientCore(openApi, '/', client, config, templates);

expect(writeFileSync).toHaveBeenCalledWith(path.resolve('/', '/OpenAPI.ts'), 'settings');
expect(writeFileSync).toHaveBeenCalledWith(path.resolve('/', '/ApiError.ts'), 'apiError');
Expand All @@ -57,7 +58,7 @@ describe('writeClientCore', () => {
});

it('uses client server value for base', async () => {
const client: Parameters<typeof writeClientCore>[0] = {
const client: Parameters<typeof writeClientCore>[2] = {
enumNames: [],
models: [],
server: 'http://localhost:8080',
Expand Down Expand Up @@ -87,7 +88,7 @@ describe('writeClientCore', () => {
write: true,
};

await writeClientCore(client, templates, '/', config);
await writeClientCore(openApi, '/', client, config, templates);

expect(templates.core.settings).toHaveBeenCalledWith({
$config: config,
Expand All @@ -98,7 +99,7 @@ describe('writeClientCore', () => {
});

it('uses custom value for base', async () => {
const client: Parameters<typeof writeClientCore>[0] = {
const client: Parameters<typeof writeClientCore>[2] = {
enumNames: [],
models: [],
server: 'http://localhost:8080',
Expand Down Expand Up @@ -129,7 +130,7 @@ describe('writeClientCore', () => {
write: true,
};

await writeClientCore(client, templates, '/', config);
await writeClientCore(openApi, '/', client, config, templates);

expect(templates.core.settings).toHaveBeenCalledWith({
$config: config,
Expand Down
6 changes: 3 additions & 3 deletions packages/openapi-ts/src/utils/write/__tests__/models.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import path from 'node:path';
import { describe, expect, it, vi } from 'vitest';

import { writeClientModels } from '../models';
import { mockTemplates } from './mocks';
import { openApi } from './models';

vi.mock('node:fs');

describe('writeClientModels', () => {
it('should write to filesystem', async () => {
const client: Parameters<typeof writeClientModels>[0] = {
const client: Parameters<typeof writeClientModels>[2] = {
enumNames: [],
models: [
{
Expand All @@ -37,7 +37,7 @@ describe('writeClientModels', () => {
version: 'v1',
};

await writeClientModels(client, mockTemplates, '/', {
await writeClientModels(openApi, '/', client, {
client: 'fetch',
debug: false,
enums: 'javascript',
Expand Down
11 changes: 11 additions & 0 deletions packages/openapi-ts/src/utils/write/__tests__/models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { OpenApi } from '../../../openApi';

export const openApi: OpenApi = {
info: {
title: '',
version: '',
},
openapi: '',
paths: {},
swagger: '',
};
57 changes: 10 additions & 47 deletions packages/openapi-ts/src/utils/write/__tests__/schemas.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,23 @@ import path from 'node:path';
import { describe, expect, it, vi } from 'vitest';

import { writeClientSchemas } from '../schemas';
import { mockTemplates } from './mocks';
import { openApi } from './models';

vi.mock('node:fs');

describe('writeClientSchemas', () => {
it('should write to filesystem', async () => {
const client: Parameters<typeof writeClientSchemas>[0] = {
enumNames: [],
models: [
{
$refs: [],
base: 'User',
description: null,
enum: [],
enums: [],
export: 'interface',
imports: [],
isDefinition: true,
isNullable: false,
isReadOnly: false,
isRequired: false,
link: null,
name: 'User',
properties: [],
template: null,
type: 'User',
if ('openapi' in openApi) {
openApi.components = {
schemas: {
foo: {
type: 'object',
},
},
],
server: 'http://localhost:8080',
services: [],
version: 'v1',
};
};
}

await writeClientSchemas(client, mockTemplates, '/', {
client: 'fetch',
debug: false,
enums: 'javascript',
experimental: false,
exportCore: true,
exportModels: true,
exportSchemas: true,
exportServices: true,
format: false,
input: '',
lint: false,
name: 'AppClient',
operationId: true,
output: '',
postfixServices: '',
serviceResponse: 'body',
useDateType: false,
useOptions: true,
write: true,
});
await writeClientSchemas(openApi, '/');

expect(writeFileSync).toHaveBeenCalledWith(path.resolve('/', '/schemas.ts'), expect.anything());
});
Expand Down
Loading