0.16.0 • Published 3 years ago

@salus-js/http v0.16.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Intro

A module for statically defining type-safe HTTP operations. These can be leveraged by other modules to build type-safe clients or servers that can execute the requests.

Usage

Defining requests with @salus-js/http is simple. Let's start with a basic GET request that has no parameters and responds with just a string.

import { t } from '@salus-js/codec'
import { http, ResponseOf } from '@salus-js/http'

const getHelloWorld = http.get('/v1/hello', {
  response: t.string
})

type HelloWorldResponse = ResponseOf<typeof getHelloWorld> // string

Most of the time, though, you'll have more exciting endpoints than this. Let's take a look at a complete description for an endpoint that creates a contact:

import { t } from '@salus-js/codec'
import { http, BodyOf, ResponseOf } from '@salus-js/http'

const contactParameters = t.partial({
  firstName: t.string.document({
    description: 'First name for the contact'
  }),
  lastName: t.string.document({
    description: 'Last name for the contact'
  })
})

const contactResource = t.object({
  object: t.literal('contact').document({
    description: 'Always `contact`.'
  }),
  id: t.string.document({
    description: 'Unique ID for the contact'
  }),
  firstName: t.string.nullable().document({
    description: 'First name for the contact'
  }),
  lastName: t.string.nullable().document({
    description: 'Last name for the contact'
  })
})

const createContact = http.post('/v1/contacts', {
  description: 'Creates a new contact.',
  body: contactParameters,
  response: contactResource
})

type CreateContactBody = BodyOf<typeof createContact> // { firstName?: string; lastName?: string }
type CreateContactResponse = ResponseOf<typeof createContact> // { object: string; id: string; firstName: string | null; lastName: string | null }

Related

Typically, you won't use @salus-js/http directly, but rather through one of the clients or servers. @salus-js/http is used internally by @salus-js/nestjs for server-side endpoints that are compatible with the NestJS framework, or with @salus-js/axios to create a type-safe HTTP client.

0.16.0

3 years ago

0.15.1

3 years ago

0.15.0

3 years ago

0.14.1

3 years ago

0.14.0

3 years ago

0.13.0

3 years ago

0.12.2

3 years ago

0.11.3

3 years ago

0.11.4

3 years ago

0.11.0

4 years ago

0.10.3

4 years ago

0.10.2

4 years ago

0.10.0

4 years ago

0.9.3

4 years ago

0.9.1

4 years ago

0.8.0

4 years ago

0.7.9

4 years ago

0.7.8

4 years ago

0.7.7

4 years ago

0.7.3

4 years ago

0.7.2

4 years ago

0.7.1

4 years ago

0.7.0

4 years ago

0.6.0

4 years ago

0.5.9

4 years ago

0.5.8

4 years ago

0.5.7

4 years ago

0.5.4

4 years ago

0.5.3

4 years ago

0.5.6

4 years ago

0.5.5

4 years ago

0.5.1

4 years ago

0.5.0

4 years ago

0.4.0-alpha.0

4 years ago

0.3.0-alpha.0

4 years ago