Skip to content

minosss/api

Repository files navigation

This is v2 of @yme/api. If you are looking for v1, please visit v1 branch.

Hey 👋, @yme/api is a package that defines the type-safe API requests. No server required and zero dependencies.

If you are developing a full-stack web application, you should take a look tRPC.

NPM version NPM Downloads Bundle Size

  • Type-Safe: Define API requests with TypeScript.
  • Zero Dependencies: No dependencies.
  • Serverless: No server required.
  • Customizable: Use your own HTTP client.
  • Middlewares: Support middlewares.
[input] -> [validator, middlewares, action, selector] -> [output]

Quick Start

import { ApiClient } from '@yme/api';
import { logger, replaceUrlParams } from '@yme/api/middleware';

const api = new ApiClient({
  action: async (config, context) => {
    // make http request and return data
    const response = await fetch(config.url, {
      method: config.method,
      headers: config.headers,
      body: JSON.stringify(config.input),
    });
    // check status or others
    return response.json();
  },
  middlewares: [
    logger,
    replaceUrlParams(),
  ],
  // handle error and return fallback data
  onError: async () => {
    return {
      message: 'Something went wrong',
      code: 233,
    };
  },

  // how to merge request config, custom for supports deep merge.
  // mergeConfig: (target, source) => {}
});

const createUser = api.post('/users', {
  // initial request config, like headers
})
  .validator(
    // input schema
    z.object({
      name: z.string(),
      age: z.number(),
    }),
  )
  .T<{ id: string; name: string; }>()
  // output schema
  .selector((user) => user.id);

const newUserId = await createUser({
  name: 'Alice',
  age: 18,
}, {
  // request config
});

License

MIT

About

define type-safe API routes simply

Resources

License

Stars

Watchers

Forks

Packages

No packages published