Skip to content

Commit

Permalink
feat: add fastify plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobinu committed Nov 14, 2024
1 parent ee3bbfb commit 5514de4
Show file tree
Hide file tree
Showing 31 changed files with 7,743 additions and 43 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-cups-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': minor
---

feat: add fastify-openapi-glue plugin
10 changes: 10 additions & 0 deletions examples/openapi-ts-fastify/openapi-ts.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from '@hey-api/openapi-ts';

export default defineConfig({
client: '@hey-api/client-fetch',
experimentalParser: true,
input:
'https://gist.githubusercontent.com/seriousme/55bd4c8ba2e598e416bb5543dcd362dc/raw/cf0b86ba37bb54bf1a6bf047c0ecf2a0ce4c62e0/petstore-v3.1.json',
output: './src/gen',
plugins: ['fastify'],
});
20 changes: 20 additions & 0 deletions examples/openapi-ts-fastify/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@example/openapi-ts-fastify",
"private": true,
"version": "0.0.1",
"type": "module",
"scripts": {
"openapi-ts": "openapi-ts",
"typecheck": "tsc --noEmit",
"test": "vitest"
},
"dependencies": {
"@hey-api/openapi-ts": "workspace:*",
"fastify": "5.0.0",
"fastify-openapi-glue": "4.7.1"
},
"devDependencies": {
"@hey-api/client-fetch": "workspace:*",
"vitest": "2.1.5"
}
}
46 changes: 46 additions & 0 deletions examples/openapi-ts-fastify/src/gen/fastify.gen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// This file is auto-generated by @hey-api/openapi-ts

import type { RouteHandler } from 'fastify';

export type Pet = {
id: number;
name: string;
tag?: string;
};

export type Pets = Array<Pet>;

export type Error = {
code: number;
message: string;
};

export type RouteHandlers = {
createPets: RouteHandler<{
Reply: {
201: unknown;
};
}>;
listPets: RouteHandler<{
Querystring?: {
/**
* How many items to return at one time (max 100)
*/
limit?: number;
};
Reply: {
200: Pets;
};
}>;
showPetById: RouteHandler<{
Params: {
/**
* The id of the pet to retrieve
*/
petId: string;
};
Reply: {
200: Pet;
};
}>;
};
3 changes: 3 additions & 0 deletions examples/openapi-ts-fastify/src/gen/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file is auto-generated by @hey-api/openapi-ts
export * from './services.gen';
export * from './types.gen';
60 changes: 60 additions & 0 deletions examples/openapi-ts-fastify/src/gen/services.gen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// This file is auto-generated by @hey-api/openapi-ts

import {
createClient,
createConfig,
type Options,
} from '@hey-api/client-fetch';

import type {
CreatePetsError,
ListPetsData,
ListPetsError,
ListPetsResponse,
ShowPetByIdData,
ShowPetByIdError,
ShowPetByIdResponse,
} from './types.gen';

export const client = createClient(createConfig());

/**
* List all pets
*/
export const listPets = <ThrowOnError extends boolean = false>(
options?: Options<ListPetsData, ThrowOnError>,
) =>
(options?.client ?? client).get<
ListPetsResponse,
ListPetsError,
ThrowOnError
>({
...options,
url: '/pets',
});

/**
* Create a pet
*/
export const createPets = <ThrowOnError extends boolean = false>(
options?: Options<unknown, ThrowOnError>,
) =>
(options?.client ?? client).post<unknown, CreatePetsError, ThrowOnError>({
...options,
url: '/pets',
});

/**
* Info for a specific pet
*/
export const showPetById = <ThrowOnError extends boolean = false>(
options: Options<ShowPetByIdData, ThrowOnError>,
) =>
(options?.client ?? client).get<
ShowPetByIdResponse,
ShowPetByIdError,
ThrowOnError
>({
...options,
url: '/pets/{petId}',
});
46 changes: 46 additions & 0 deletions examples/openapi-ts-fastify/src/gen/types.gen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// This file is auto-generated by @hey-api/openapi-ts

export type Pet = {
id: number;
name: string;
tag?: string;
};

export type Pets = Array<Pet>;

export type Error = {
code: number;
message: string;
};

export type ListPetsData = {
body?: never;
path?: never;
query?: {
/**
* How many items to return at one time (max 100)
*/
limit?: number;
};
};

export type ListPetsError = Error;

export type ListPetsResponse = Pets;

export type CreatePetsError = Error;

export type ShowPetByIdData = {
body?: never;
path: {
/**
* The id of the pet to retrieve
*/
petId: string;
};
query?: never;
};

export type ShowPetByIdError = Error;

export type ShowPetByIdResponse = Pet;
14 changes: 14 additions & 0 deletions examples/openapi-ts-fastify/src/handlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { Pet, RouteHandlers } from './gen/fastify.gen';

export const serviceHandlers: Pick<RouteHandlers, 'showPetById'> = {
showPetById(request, reply) {
const {
params: { petId },
} = request;
const pet: Pet = {
id: Number(petId),
name: 'petname',
};
reply.code(200).send(pet);
},
};
10 changes: 10 additions & 0 deletions examples/openapi-ts-fastify/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { buildServer } from './server';

const fastify = buildServer();

fastify.listen({ port: 3000 }, function (err) {
if (err) {
fastify.log.error(err);
process.exit(1);
}
});
23 changes: 23 additions & 0 deletions examples/openapi-ts-fastify/src/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Fastify from 'fastify';
import glue from 'fastify-openapi-glue';

import { serviceHandlers } from './handlers';

export const buildServer = () => {
const fastify = Fastify();

const specification = fetch(
'https://gist.githubusercontent.com/seriousme/55bd4c8ba2e598e416bb5543dcd362dc/raw/cf0b86ba37bb54bf1a6bf047c0ecf2a0ce4c62e0/petstore-v3.1.json',
)
.then((reply) => reply.json())
.then((data) => data);
console.log(specification);

fastify.register(glue, {
prefix: 'v3',
serviceHandlers,
specification,
});

return fastify;
};
31 changes: 31 additions & 0 deletions examples/openapi-ts-fastify/test/pets.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { type FastifyInstance } from 'fastify';
import { client, showPetById } from 'src/gen';
import { buildServer } from 'src/server';
import { afterAll, beforeAll, describe, expect, test } from 'vitest';

describe('/pet/findByTags', () => {
let server: FastifyInstance;

beforeAll(async () => {
server = buildServer();
await server.listen();

// @ts-ignore
const baseUrl = `http://localhost:${server.server.address().port}/v3`;
client.setConfig({ baseUrl });
});

afterAll(async () => {
await Promise.all([server.close()]);
});

test('showPetById', async () => {
const result = await showPetById({
client,
path: {
petId: '123',
},
});
expect(result.response.status).toBe(200);
});
});
13 changes: 13 additions & 0 deletions examples/openapi-ts-fastify/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"include": ["src/**/*", "test/**/*"],
"compilerOptions": {
"lib": ["es2023"],
"target": "es2022",
"module": "ESNext",
"moduleResolution": "Bundler",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"baseUrl": "."
}
}
1 change: 1 addition & 0 deletions packages/openapi-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
"cross-spawn": "7.0.3",
"eslint": "9.6.0",
"express": "4.21.0",
"fastify": "5.0.0",
"glob": "10.4.3",
"node-fetch": "3.3.2",
"prettier": "3.3.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
} from '../../@hey-api/services/plugin-legacy';
import { typesId } from '../../@hey-api/types/plugin';
import type { PluginHandler } from '../../types';
import { schemaToType, type SchemaToTypeOptions } from '../../utils/types';
import type { Config as AngularQueryConfig } from '../angular-query-experimental';
import type { Config as ReactQueryConfig } from '../react-query';
import type { Config as SolidQueryConfig } from '../solid-query';
Expand Down
20 changes: 20 additions & 0 deletions packages/openapi-ts/src/plugins/fastify/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { DefineConfig, PluginConfig } from '../types';
import { handler } from './plugin';
import type { Config } from './types';

export const defaultConfig: PluginConfig<Config> = {
_dependencies: ['@hey-api/services'],
_handler: handler,
_handlerLegacy: () => {},
name: 'fastify',
operationId: true,
output: 'fastify',
};

/**
* Type helper for the Fastify plugin, returns {@link PluginConfig} object
*/
export const defineConfig: DefineConfig<Config> = (config) => ({
...defaultConfig,
...config,
});

Check warning on line 20 in packages/openapi-ts/src/plugins/fastify/config.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/fastify/config.ts#L18-L20

Added lines #L18 - L20 were not covered by tests
2 changes: 2 additions & 0 deletions packages/openapi-ts/src/plugins/fastify/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { defaultConfig, defineConfig } from './config';
export type { Config } from './types';
Loading

0 comments on commit 5514de4

Please sign in to comment.