0.1.8 • Published 4 months ago

@yasanchezz/openapi-typescript-interface v0.1.8

Weekly downloads
-
License
ISC
Repository
github
Last release
4 months ago

@yasanchezz/openapi-typescript-interface

If you wanna use openapi-typescript with union entry point such as union request function, you can install this package and get swagger types for your own data fetching function.

import type { GetSwaggerInterface } from '@yasanchezz/openapi-typescript-interface';
import type { paths } from '../swagger'; // or where do you place swagger types?

// user input values
type UserInput = {
  sginal?: AbortSignal | null;
};

// user output values
type UserOutput = {
  something: string;
}

const request: GetSwaggerInterface<paths, UserInput, UserOutput> = async (options): Promise<any> => {
  const response = await fetch(options.path, {
     body: 'body' in options && 'headers' in options
      ? JSON.stringify(options.body) // for example
      : null,
    cache: 'no-cache',
    headers: new Headers(options.headers),
    method: options.method.toUpperCase(),
    mode: 'cors',
    signal: options.signal || null,
  });

  const body = await response.json();

  return {
    status: response.status, // library required field
    body, // library required field
    ok: response.ok, // library required field
    something: 'something', // UserOutput.something
  };
};

// all passed and response values will be inference from swagger types
const response = await request({
  headers: { 'Content-Type': 'application/json' },
  method: 'delete',
  path: `/api/v1/document/${id}`,
});

// success
if (response.ok) {
  response.body;
}
0.1.8

4 months ago

0.1.7

11 months ago

0.1.6

1 year ago

0.1.5

1 year ago

0.1.4

1 year ago

0.1.3

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago