Skip to content

Commit

Permalink
feat(types): add support for openapi webhooks
Browse files Browse the repository at this point in the history
Generate type for the webhooks operations too.

fix #38
  • Loading branch information
nfroidure committed Oct 17, 2024
1 parent 3d515a4 commit 6eb1222
Show file tree
Hide file tree
Showing 4 changed files with 476 additions and 285 deletions.
47 changes: 47 additions & 0 deletions fixtures/openapi/webhook_example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"openapi": "3.1.0",
"info": {
"title": "Webhook Example",
"version": "1.0.0"
},
"webhooks": {
"newPet": {
"post": {
"requestBody": {
"description": "Information about a new pet in the system",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Pet"
}
}
}
},
"responses": {
"200": {
"description": "Return a 200 status to indicate that the data was received successfully"
}
}
}
}
},
"components": {
"schemas": {
"Pet": {
"required": ["id", "name"],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
},
"tag": {
"type": "string"
}
}
}
}
}
}
108 changes: 108 additions & 0 deletions src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3909,6 +3909,114 @@ export namespace Enums {
}"
`;

exports[`generateOpenAPITypes() with OpenAPI samples should work with webhook_example.json 1`] = `
"declare namespace API {
export namespace PostWebhooksNewPet {
export type Body = Components.RequestBodies.PostWebhooksNewPetRequestBody;
export type Output = Responses.$200;
export type Input = {
readonly body?: Body;
};
export namespace Responses {
export type $200 = Components.Responses.PostWebhooksNewPetResponse200<200>;
}
}
}
declare namespace Components {
export namespace RequestBodies {
export type PostWebhooksNewPetRequestBody = Components.Schemas.Pet;
}
export namespace Responses {
export type PostWebhooksNewPetResponse200<S extends number> = {
readonly status: S;
readonly headers?: {
readonly [name: string]: unknown;
};
readonly body?: unknown;
};
}
export namespace Schemas {
export type Pet = {
id: number;
name: string;
tag?: string;
};
}
}"
`;

exports[`generateOpenAPITypes() with OpenAPI samples should work with webhook_example.json and filterStatuses 200/201/202/300 and brandedTypes 1`] = `
"declare namespace API {
export namespace PostWebhooksNewPet {
export type Body = Components.RequestBodies.PostWebhooksNewPetRequestBody;
export type Output = Responses.$200;
export type Input = {
readonly body?: Body;
};
export namespace Responses {
export type $200 = Components.Responses.PostWebhooksNewPetResponse200<200>;
}
}
}
declare namespace Components {
export namespace RequestBodies {
export type PostWebhooksNewPetRequestBody = Components.Schemas.Pet;
}
export namespace Responses {
export type PostWebhooksNewPetResponse200<S extends number> = {
readonly status: S;
readonly headers?: {
readonly [name: string]: unknown;
};
readonly body?: unknown;
};
}
export namespace Schemas {
export type Pet = {
id: number;
name: string;
tag?: string;
};
}
}"
`;

exports[`generateOpenAPITypes() with OpenAPI samples should work with webhook_example.json and generateUnusedSchemas option to true 1`] = `
"export namespace AnotherAPI {
export namespace PostWebhooksNewPet {
export type Body = Components.RequestBodies.PostWebhooksNewPetRequestBody;
export type Output = Responses.$200;
export type Input = {
readonly body?: Body;
};
export namespace Responses {
export type $200 = Components.Responses.PostWebhooksNewPetResponse200<200>;
}
}
}
export namespace Components {
export namespace RequestBodies {
export type PostWebhooksNewPetRequestBody = Components.Schemas.Pet;
}
export namespace Responses {
export type PostWebhooksNewPetResponse200<S extends number> = {
readonly status: S;
readonly headers?: {
readonly [name: string]: unknown;
};
readonly body?: unknown;
};
}
export namespace Schemas {
export type Pet = {
id: number;
name: string;
tag?: string;
};
}
}"
`;

exports[`generateOpenAPITypes() with OpenAPI samples should work with whook_example.json 1`] = `
"declare namespace API {
export namespace GetDelay {
Expand Down
21 changes: 10 additions & 11 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from './index.js';
import { readFileSync, readdirSync } from 'fs';
import path from 'path';
import { OpenAPIV3 } from 'openapi-types';
import { OpenAPIV3_1 } from 'openapi-types';
import type { JSONSchema7 } from 'json-schema';
import type { Context } from './index.js';

Expand Down Expand Up @@ -79,7 +79,7 @@ describe('generateOpenAPITypes()', () => {
},
},
},
} as OpenAPIV3.Document;
} as OpenAPIV3_1.Document;

expect(
toSource(
Expand Down Expand Up @@ -231,7 +231,7 @@ export namespace Components {
},
},
},
} as OpenAPIV3.Document;
} as OpenAPIV3_1.Document;

expect(
toSource(
Expand Down Expand Up @@ -299,7 +299,7 @@ declare namespace Components {
test(`should work with ${file}`, async () => {
const schema = JSON.parse(
readFileSync(path.join(fixturesDir, file)).toString(),
) as OpenAPIV3.Document;
) as OpenAPIV3_1.Document;

expect(
toSource(
Expand All @@ -315,7 +315,7 @@ declare namespace Components {
test(`should work with ${file} and filterStatuses 200/201/202/300 and brandedTypes`, async () => {
const schema = JSON.parse(
readFileSync(path.join(fixturesDir, file)).toString(),
) as OpenAPIV3.Document;
) as OpenAPIV3_1.Document;

expect(
toSource(
Expand All @@ -333,7 +333,7 @@ declare namespace Components {
test(`should work with ${file} and generateUnusedSchemas option to true`, async () => {
const schema = JSON.parse(
readFileSync(path.join(fixturesDir, file)).toString(),
) as OpenAPIV3.Document;
) as OpenAPIV3_1.Document;

expect(
toSource(
Expand Down Expand Up @@ -1244,9 +1244,8 @@ describe('generateTypeDeclaration()', () => {
},
};

expect(
toSource(await generateTypeDeclaration(context, schema))
).toMatchInlineSnapshot(`
expect(toSource(await generateTypeDeclaration(context, schema)))
.toMatchInlineSnapshot(`
"export type Unknown = {
"1.0"?: number;
"5.0"?: number;
Expand Down Expand Up @@ -1289,7 +1288,7 @@ describe('generateTypeDeclaration()', () => {
},
},
},
} as OpenAPIV3.Document;
} as OpenAPIV3_1.Document;

expect(
toSource(
Expand Down Expand Up @@ -1349,7 +1348,7 @@ declare namespace Components {
},
},
},
} as OpenAPIV3.Document;
} as OpenAPIV3_1.Document;

expect(
toSource(
Expand Down
Loading

0 comments on commit 6eb1222

Please sign in to comment.