4.0.0 • Published 7 months ago

get-scrud v4.0.0

Weekly downloads
4
License
MIT
Repository
github
Last release
7 months ago

get-scrud NPM version js-standard-style Dependency Status

Client for SCRUD style rest APIs

Install

$ npm install --save get-scrud

2.0.0

This is a very breaking change, switching to ES Modules and eliminating babel. If you are targeting older browsers you will need to include this module in any babel-ification you're doing.

API

Module ships single function which accepts options and returns main function

const apiCall = require('get-scrud')(*opts)

Main function returns Promise that resolves with JSON parsed response data

apiCall(*resource, *action, [*id, *body, *jwt])

Alternatively address each action with apiCall[action](...)

apiCall.[search, create, read, update, delete](*resource, [*id, *body, *jwt])

Change options on an instance

If you need to change any options on an existing instance, you can call the main function with a single argument, which is the new options you want to merge (Object.assign) with the original. That call will be synchronous.

apiCall(*opts)

Usage

const opts = {
  host: 'jsonplaceholder.typicode.com',
  port: 443,
  timeout: '3m', // or '180s' or 180000
  jwt: `abc123`
}
const apiCall = require('get-scrud')(opts)
const body = {userId: 1, title: `get scrud yo`, body: `you're scrud`}
const jwt = `def456`

async function callApis () {

  /* no jwt passed, uses jwt set in init if one was set */

  // SEARCH
  let search = await apiCall('posts', 'search', {userId: 1})
  // CREATE
  let create = await apiCall('posts', 'create', body)
  // READ
  let read = await apiCall('posts', 'read', 1)
  // UPDATE
  let update = await apiCall('posts', 'update', 1, {userId: 5})
  // DELETE
  let deleted = !!(await apiCall('posts', 'delete', 2))

  /* passing in jwt to call, overrides jwt set in init if one was set */

  // SEARCH
  let search = await apiCall('posts', 'search', {userId: 1}, jwt)
  // CREATE
  let create = await apiCall('posts', 'create', body, jwt)
  // READ
  let read = await apiCall('posts', 'read', 1, jwt)
  // UPDATE
  let update = await apiCall('posts', 'update', 1, {userId: 5}, jwt)
  // DELETE
  let deleted = !!(await apiCall('posts', 'delete', 2, jwt))

}

Related

scrud - Super opinionated, minimalistic, PG centric API fabric

License

MIT © Andrew Carpenter

4.0.0

7 months ago

3.2.1

8 months ago

3.1.0

9 months ago

3.0.0

10 months ago

3.0.0-beta.1

2 years ago

1.9.1

2 years ago

1.9.0

2 years ago

2.0.0

2 years ago

1.9.2

2 years ago

1.8.1

2 years ago

1.8.0

2 years ago

1.7.1

2 years ago

1.7.0

3 years ago

1.6.2

3 years ago

1.6.1

3 years ago

1.6.0

3 years ago

1.5.2

3 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.4.6

4 years ago

1.4.5

4 years ago

2.0.0-2

4 years ago

2.0.0-1

4 years ago

1.4.4

4 years ago

1.4.3

5 years ago

1.4.2

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.5

5 years ago

1.3.4

6 years ago

1.3.3

6 years ago

1.3.2

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.7

6 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.1.0

7 years ago