1.0.1-alpha.1 • Published 4 years ago

fetch-repository v1.0.1-alpha.1

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

Fetch Repository

This is a simple library that consists in a fetch wrapper + repository pattern utility. You can use both together or in isolation.

Warning: This is a fresh idea that is evolving, and may contain bugs.

  1. Using the Fetch Wrapper
  2. Using the Repository API

Using the fetch wrapper

import { http } from 'fetch-repository'

// With promise
http
  .get(url, config)
  .then(result => {})
  .catch(err => {})

// With async/await
async function getData() {
  try {
    const result = await http.get(url, config)
  } catch (e) {
    console.error(e)
  }
}

Using the repository api

import { http, Repository } from 'fetch-repository'

Repository.add('Posts', {
  get baseUrl() {
    return 'https://jsonplaceholder.typicode.com/posts'
  },
  getAll() {
    return http.get(this.baseUrl)
  },
  getById(id: number) {
    return http.get(`${this.baseUrl}/${id}`)
  },
  create(body: object) {
    return http.post(this.baseUrl, body)
  },
})

Then, just consume it later:

import { Repository } from 'fetch-repository'

const PostsRepository = Repository.get('Posts')

try {
  const posts = await PostsRepository.getAll()
} catch (e) {
  console.error(e)
}
1.0.1-alpha.1

4 years ago

1.0.1-alpha.0

4 years ago

1.0.4

4 years ago

1.0.2

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago