@sdk-it/generic v0.21.0
@sdk-it/generic
This package provides tools to analyze TypeScript code and generate OpenAPI specifications from it. It can extract route information, parameter types, and response schemas from your TypeScript codebase.
Frameworks specific integrations
Installation
npm install @sdk-it/genericUsage
Consider the following example:
- Create a route using your API framework of choice with the
@openapitag and validate middleware.
import z from 'zod';
import { validate } from '@sdk-it/express/runtime';
const app = express();
/**
* @openapi getAuthor
* @tags authors
*/
app.get(
'/authors/:id',
validate((payload) => ({
id: {
select: payload.param.id,
against: z.string(),
},
})),
async (req, res) => {
const author = [{ name: 'John Doe' }];
return res.json(author);
},
);- Use the generate fn to create an OpenAPI spec from your routes.
import { join } from 'node:path';
import { analyze, responseAnalyzer } from '@sdk-it/generic';
import { generate } from '@sdk-it/typescript';
const { paths, components } = await analyze('path/to/tsconfig.json', {
responseAnalyzer,
});
// Now you can use the generated specification to create an SDK or save it to a file
const spec = {
info: {
title: 'My API',
version: '1.0.0',
},
paths,
components,
};
await generate(spec, {
output: join(process.cwd(), './client'),
});!TIP See typescript for more info.
Customizing Operations
You can customize the operations as well as add more through the onOperation fn.
Use file name as tag
Assuming your projects structurd like the following where routes are grouped by representivie file names.
apps/
backend/
tsconfig.app.json
src/
routes/
authors.tsThen you can consider the file name as the tag for the operation which means you don't need to specify the tag in the JSDoc comment or only specify the tag if you want to override the default behavior.
import { basename } from 'node:path';
import { camelcase } from 'stringcase';
import { analyze, responseAnalyzer } from '@sdk-it/generic';
const { paths, components } = await analyze('apps/backend/tsconfig.app.json', {
responseAnalyzer,
onOperation(sourceFile, method, path, operation) {
const fileName = basename(sourceFile.split('/').at(-1), '.ts');
return {
[method]: {
[path]: {
...operation,
tags: [fileName],
},
},
};
},
});6 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago