0.10.2 • Published 5 years ago

@supersimplethings/fetchme v0.10.2

Weekly downloads
26
License
MIT
Repository
github
Last release
5 years ago

@supersimplethings/fetchme

npm (scoped)

Things should be simple to use

Polite fetch library.

Usage

async function getResponse(someArgs) {
  const body = {
    some: someArgs.some,
    args: someArgs.args
  }
  const response = await fetchme().post(body).to('https://some.site/endpoint').plz()
}

Installation

With npm:

npm install @supersimplethings/fetchme

With yarn:

yarn add @supersimplethings/fetchme

Advanced usage

const ourApi = {
  name: 'ourApi',
  domain: 'https://some.domain',
  endpoints: {
    userById: userId => `/users/${userId}`
  }
}

const token = 'some_token'

const fetchme = new Fetchme(ourApi)
  .setOptions({ headers: { Authorization: `Bearer: ${token}` }})
  .addMiddleware('body', someBodyParser)

async function updateUser(someArgs, userId) {
  const body = {
    some: someArgs.some,
    args: someArgs.args
  }
  const user = await fetchme.put(body).to('ourApi').userById.with(userId).plz()
}

Repository pattern

import { Fetchme, Repository } from '@supersimplethings/fetchme'

class UsersRepository extends Repository {
  constructor(apis, Fetcher) {
    super(apis, Fetcher)

    this.apis = apis
    this.fetcher = new Fetcher(apis)
      .setOptions({ headers: { Authorization: `Bearer: ${token}` }})
      .addMiddleware('body', someBodyParser)
  }

  async createUser(someArgs) {
    const body = {
      some: someArgs.some,
      args: someArgs.args
    }
    const response = await this.fetcher.post(body).to().user.create.plz()

    if (!response.success) return []
    const newUser = someParse(response.data)
    return newUser
  }
}

const ourApi = {
  name: 'ourApi',
  domain: 'https://some.domain',
  endpoints: {
    user: {
      create: () => '/users'
    }
  }
}

const usersRepository = new UsersRepository(ourApi, Fetchme)

License

MIT

0.10.2

5 years ago

0.10.1

5 years ago

0.10.0

5 years ago

0.9.4

5 years ago

0.9.3

5 years ago

0.9.2

5 years ago

0.9.2-23

5 years ago

0.9.2-22

6 years ago

0.9.2-21

6 years ago

0.9.2-20

6 years ago

0.9.2-19

6 years ago

0.9.2-18

6 years ago

0.9.2-17

6 years ago

0.9.2-16

6 years ago

0.9.2-15

6 years ago

0.9.2-14

6 years ago

0.9.2-13

6 years ago

0.9.2-12

6 years ago

0.9.2-11

6 years ago

0.9.2-10

6 years ago

0.9.2-9

6 years ago

0.9.2-8

6 years ago

0.9.2-7

6 years ago

0.9.2-5

6 years ago

0.9.2-3

6 years ago

0.9.2-4

6 years ago

0.9.2-2

6 years ago

0.0.10

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago