You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ojizero 277dcdd9a7
more on documentations, still more documenting is needed
1 year ago
examples first tested example added 1 year ago
src first tested example added 1 year ago
test more on types hell 1 year ago
.gitignore futher setup and module skeleton 1 year ago
.node-version client source 1 year ago
.npmignore client source 1 year ago
LICENSE Initial commit 1 year ago
README.md more on documentations, still more documenting is needed 1 year ago
package-lock.json client skeleton 1 year ago
package.json refactor typing in resourcefactory 1 year ago
tsconfig.json resource class and tests 1 year ago
tsconfig.spec.json restructure method, split client into interface and implementation 1 year ago

README.md

# portal > HTTP API clients ... simplified.

Installation

npm install --save @ojizero/portal

Usage

Aimed to be used as a building block for API client libraries

/// In your library or definition file
import portal from '@ojizero/portal'

const client = portal({ baseUrl: 'some.base.url' }) // Initial configuration can be passed here

// Get method without path variables
export const someGetMethod = client.route({ path: '/some/path' })
// Get method with path variables
export const someGetMethodWithParam = client.route({ path: '/some/path/:withInnerVariable' })

/// NOTE: ideally this wouldn't be a module level instance but this is to simplify this example 😬

/* ******************* */

/// In your application
import YourAPIClient from 'your-client-module'

const someGetMethodPromise = YourAPIClient.someGetMethod() // GET http://some.base.url/some/path
const someGetMethodWithParamPromise = YourAPIClient.someGetMethodWithParam(5) // GET http://some.base.url/some/path/5

API Documentation

(default export) createPortalClient(config)

Used to create a portal base client. Returns a portal object

config

All config options are required unless otherwise stated.

baseUrl

Type: string (required).

The base URL used by the client, all related URIs are prepended by it.

headers

Type: OutgoingHttpHeaders

The default headers to be attached to all requests.

authentication

Type: Authentication

Authentication
# type

Type: AuthenticationTypes (required)

Valid AuthenticationTypes are:

  • None = ‘none’
    • No authentication required. (same as not providing the authentication option)
  • BasicAuth = ‘basic’,
    • Use username and password to generate a token.
    • Token is added to the Authorization header prepended with basic.
  • BearerAuth = ‘bearer’,
    • Use authToken as is.
    • Token is added to the Authorization header prepended with bearer.
# username

Type: string

Required if using BasicAuth type.

# password

Type: string

Required if using BasicAuth type.

# authToken

Type: string

Required if using BearerAuth type.

retries

Type: number

Number of retries to execute on failures, default is 0.

timeout

Type: number

Request timeout in seconds, default is 30

onError

Type: 'reject' | 'resolve'

TODO: I think it’s not working currently 🤔

If set to resolve error won’t throw, instead will return a normal response, default is reject.

Portal Object

The returns object has three attributes

route

Which is a MethodFactory, it can be used to generate client routes.

resource

Which is a ResourceFactory, it can be used to generate client resources.

_client

Which is the underlying client instance used by the factories.

Status

This is still a work in progress :D any help is appreciated

License

MIT licensed.