Install
npm install @rocket-scripts/openapi
Generate TypeScript Clients
{
"scripts": "openapi-typescript-generator -i your-openapi-spec.yaml -o src/client"
}
See detail: https://github.com/rocket-hangar/openapi-typescript-generator/tree/master/reference/src/basic
import { APIExceptionError } from '@rocket-scripts/openapi';
import { defaultApi, SomeData, SomeException } from './client';
const config = {
basePath: server.getBasePath(),
};
try {
const result: SomeData = await defaultApi.someGet(config);
console.log(result.hello);
} catch (error: unknown) {
if (error instanceof APIExceptionError) {
const exception: SomeException = error.exception;
console.error(exception);
}
}
import { APIExceptionError, pipe, fetchRequest } from '@rocket-scripts/openapi';
import { defaultApi, SomeData } from './client';
const config = {
basePath: server.getBasePath(),
};
const result: SomeData = await pipe(
defaultApi.someGetRequest(config),
fetchRequest(),
defaultApi.someGetReponse(),
)();
// or
const result: SomeData = await pipe(
fetchRequest(),
defaultApi.someGetReponse(),
)({ url: 'http://custom/url/path', init: {} });
// or
const response: Response = await pipe(
defaultApi.someGetRequest(config),
fetchRequest(),
)();
import {
APIExceptionError,
pipe,
fetchRequest,
takeResponse,
} from '@rocket-scripts/openapi';
import { defaultApi, SomeData } from './client';
const config = {
basePath: server.getBasePath(),
};
const { value, response }: { value: SomeData; response: Response } = await pipe(
defaultApi.someGetRequest(config),
fetchRequest(),
takeResponse(defaultApi.someGetReponse()),
)();