Coder Social home page Coder Social logo

ts-fetch's Introduction

ts-fetch

npm version GitHub version CircleCI PRs Welcome

Small wrapper around fetch making it possible to have type safety around network requests. By passing generics it's possible to indicate which types are expected on success/error and work directly with those types. ts-fetch is slightly opinionated, but it's possible to override all settings. These are the default settings:

const defaultRequestParams = {
  method: 'GET',
  jsonRequest: true,
  jsonResponse: true,
  validStatusCodeStart: 200,
  validStatusCodeEnd: 299,
  timeout: 12000, // 12 seconds default timeout
}

If the response is not a valid JSON response and the jsonResponse was set to true, the request would return an error.

Example usages

Basic request with no arguments

const response = await request<{name: string}, {errorCode: number}>({
  url: 'https://myapi.com'
})
if (response.status === 'OK') {
  // Work with response.data in a typesafe way ๐Ÿ‘
}

Request with custom arguments and a non-JSON response

const response = await request<never, { errorCode: number }>({
  url: 'https://myapi.com',
  body: { name: 'Updated name of user' },
  method: 'PUT',
  jsonResponse: false, // Response will not be in JSON
  timeout: 1000, // Only 1 second timeout
  validStatusCodes: [201], // Only 201 indicates success
  extraHeaders: [{ key: 'Secret', value: '2lknf3oihvls' }],
})
if (response.status === 'OK') {
  // Things went well ๐Ÿ‘
} else if (response.status === 'NETWORK_ERROR') {
  // Handle network error
} else {
  // Work with the returned error data that you expect in your response
}

More examples will follow ...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.