New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

service-creator

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

service-creator

A simple abstraction to create "services", plain objects that can be used to perform fetch calls in a convention over configuration fashion.

latest
npmnpm
Version
0.6.0
Version published
Maintainers
1
Created
Source

NPM Version License: MIT CI

service-creator

A simple abstraction to create "services" — plain objects with typed async methods that perform fetch calls, using convention over configuration.

Installation

npm install service-creator

Usage

Define your service endpoints with createEndpoint<TResponse, TArgs?>() for full type safety:

import { createService, createEndpoint } from 'service-creator';

interface User {
  id: string;
  name: string;
}

const fetcher = {
  fetch: async (url, opts) => {
    const resp = await fetch(url, opts);
    return resp.json();
  },
};

const userService = createService({
  endpoints: {
    // No args — just one generic for the response type
    listUsers: createEndpoint<User[]>({
      url: '/v1/users',
    }),

    // With args — response type first, then args type
    getUser: createEndpoint<User, { id: string }>({
      url: ({ id }) => `/v1/users/${id}`,
    }),

    // POST with body
    createUser: createEndpoint<User, { name: string }>({
      url: '/v1/users',
      method: 'POST',
      body: (args) => args, // args is typed as { name: string }
    }),

    // Response transformation
    getUserName: createEndpoint<string, { id: string }>({
      url: ({ id }) => `/v1/users/${id}`,
      transform: (data) => data.name, // raw response → typed return value
    }),
  },
  basePath: 'https://api.example.com',
  fetcher,
});

// All types are fully inferred:
const users = await userService.listUsers();                  // User[]
const user = await userService.getUser({ id: '123' });        // User
const created = await userService.createUser({ name: 'Ali' });// User
const name = await userService.getUserName({ id: '123' });    // string

createEndpoint<TResponse, TArgs?, TError?>

GenericDescriptionDefault
TResponseReturn type (Promise<TResponse>)any
TArgsInput parameter type. Omit for no-arg endpoints.void
TErrorError type (available via InferError)Error

Descriptor options

OptionTypeDescription
urlstring | (args: TArgs) => stringEndpoint URL — static or dynamic
methodHttpMethodHTTP method (GET, POST, PUT, DELETE, etc.)
bodyobject | (args: TArgs) => anyRequest body — static or computed from args
headersobject | (args: TArgs) => anyRequest headers — static or computed from args
paramsobject | (args: TArgs) => anyQuery string params — static or computed from args
fetchOptsFetchOptionsAdditional fetch options (credentials, mode, etc.)
transform(data: any) => TResponseTransform the raw response before returning

Custom error types

interface ApiError {
  code: number;
  message: string;
}

const service = createService({
  endpoints: {
    riskyCall: createEndpoint<Data, { id: string }, ApiError>({
      url: ({ id }) => `/v1/data/${id}`,
    }),
  },
  fetcher,
});

Alternative styles

Plain descriptors (types inferred from function signatures):

const service = createService({
  endpoints: {
    getUser: {
      url: (id: string) => `/v1/users/${id}`,
      transform: (data: any): User => data,
    },
  },
  fetcher,
});

Legacy explicit interface:

interface MyService {
  getUser: (id: string) => Promise<User>;
}

const service = createService<MyService>({
  endpoints: {
    getUser: { url: (id: string) => `/v1/users/${id}` },
  },
  fetcher,
});

License

MIT

Keywords

create-service

FAQs

Package last updated on 27 Mar 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts