@rocket-scripts/openapi
TypeScript icon, indicating that this package has built-in type declarations

0.3.0 • Public • Published

OpenAPI TypeScript Fetch Generator

Install and Generate Clients

Install

npm install @rocket-scripts/openapi

Generate TypeScript Clients

{
  "scripts": "openapi-typescript-generator -i your-openapi-spec.yaml -o src/client"
}

Use Generated Clients

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);
  }
}

Reorganize Composition

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(),
)();

Take Serialize data with Response

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()),
)();

Package Sidebar

Install

npm i @rocket-scripts/openapi

Weekly Downloads

1

Version

0.3.0

License

MIT

Unpacked Size

75 kB

Total Files

63

Last publish

Collaborators

  • ssen