0.3.0 • Published 3 years ago

@rocket-scripts/openapi v0.3.0

Weekly downloads
42
License
MIT
Repository
github
Last release
3 years ago

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

3 years ago

0.2.0

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago