npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

typed-api-mapper

v1.1.9

Published

Type safe and organized client to map your API.

Downloads

4

Readme

npm version

TypedApiMapper

Type safe and organized client to map your API.

Installation

npm

npm install typed-api-mapper

yarn

yarn add typed-api-mapper

Usage

TypedApiMapper allow you to create an easy to use object which map all your API calls.

import { Route, createApi } from 'typed-api-mapper';

// Create the API map
const api = createApi<{ todos: { get: Route<{input: { id: number }; output: Todo}> } }>(requestHandler, {
    todos: todosRoutes,
});

// Use the API map object
const todo = await api.todos.get({ id: 1 });

Map your routes

Before creating the API map object you need to map the type of each route specifing the Input and the Output of the route. To do this you can use the Route type.

type Todo = {
  id: number;
  title: string;
};

type TodosRoutes = {
  get: Route<{input: Pick<Todo, 'id'>; output: Todo}>
  list: Route<{output: Todo[]}>
  create: Route<{input: Omit<Todo, 'id'>; output: Todo}>
  delete: Route<{input: Pick<Todo, 'id'>}>
};

Then you have to group your routes using the createApiRoutes function which will return a ApiRoutes object. This function require as parameter an object with specified the options for each route.

type ApiOptions = {
  method: 'GET' | 'POST' | 'DELETE'
  name: string
};

const todosRoutes = createApiRoutes<TodosRoutes, ApiOptions>({
  get: {method: 'GET', name: 'GetTodo'},
  list: {method: 'GET', name: 'ListTodos'},
  create: {method: 'POST', name: 'CreateTodo'},
  delete: {method: 'DELETE', name: 'DeleteTodo'},
});

Request handler

The request handler is the action that will be executed on each route. The function has to be of type RequestHandler.

The parameters are: path: it is the path of the route. for example if you call api.todos.get() the value of path will be ["todos", "get"]. options: it contains the object specified when createApiRoutes was called. data: it is the input data of the route and it is of type specified in the Route type.

function requestHandler<TInput, TOutput>(
  path: string[],
  options: ApiOptions,
  data?: TInput,
): Promise<TOutput> {
  console.log(`Request: ${options.name}`);
  console.log(`Path: ${path}`);
  console.log(`Data: ${JSON.stringify(data)}`);
  return Promise.resolve({} as TOutput);
}

Request handler

Finally we can create an API map object using the createApi function which will return a Api object.

import { Route, createApiRoutes, createApi } from 'typed-api-mapper';

type Todo = {
  id: number;
  title: string;
};

type TodosRoutes = {
  get: Route<{input: Pick<Todo, 'id'>; output: Todo}>
  list: Route<{output: Todo[]}>
  create: Route<{input: Omit<Todo, 'id'>; output: Todo}>
  delete: Route<{input: Pick<Todo, 'id'>}>
};

type ApiOptions = {
  method: 'GET' | 'POST' | 'DELETE'
  name: string
};

const todosRoutes = createApiRoutes<TodosRoutes, ApiOptions>({
  get: {method: 'GET', name: 'GetTodo'},
  list: {method: 'GET', name: 'ListTodos'},
  create: {method: 'POST', name: 'CreateTodo'},
  delete: {method: 'DELETE', name: 'DeleteTodo'},
});

function requestHandler<TInput, TOutput>(
  path: string[],
  options: ApiOptions,
  data?: TInput,
): Promise<TOutput> {
  console.log(`Request: ${options.name}`);
  console.log(`Path: ${path}`);
  console.log(`Data: ${JSON.stringify(data)}`);
  return Promise.resolve({} as TOutput);
}

type ApiMap = {
    todos: TodosRoutes
};
  
const api = createApi<ApiMap, ApiOptions>(requestHandler, {
    todos: todosRoutes,
})

api.todos.create({ title: 'Wash the dishes' })
.then(() => console.log('Todo created!'));

/*
  OUTPUT:

  Request: CreateTodo
  Path: todos,create
  Data: {"title":"Wash the dishes"}
  Todo created!
*/