Options
All
  • Public
  • Public/Protected
  • All
Menu

@egomobile/oauth2-client

Index

Type aliases

AxiosFactory

AxiosFactory: () => Promise<AxiosInstance>

Type declaration

    • (): Promise<AxiosInstance>
    • A function that provides an axios instance.

      Returns Promise<AxiosInstance>

NewAxiosClientConfig

NewAxiosClientConfig: NewAxiosClientConfig2

Configuration for a new axios client.

Nilable

Nilable<T>: Nullable<T> | Optional<T>

A type that can be (null) or (undefined).

Type parameters

  • T: any = any

Nullable

Nullable<T>: T | null

A type that can also be (null).

Type parameters

  • T: any = any

Optional

Optional<T>: T | undefined

A type that can also be (undefined).

Type parameters

  • T: any = any

Functions

createClientCredentialsClient

  • Creates a new pre-configured axios instance, that first gets an access token via an OAuth 2.0 workflow of type 'client_credentials'.

    example
    import { createClientCredentialsClient } from '@egomobile/oauth2-client'

    const client = await createClientCredentialsClient({
    // data for client authorization
    auth: {
    clientId: 'foo',
    clientSecret: 'bar'
    },

    // base URL of the API
    baseURL: 'https://api.example.com',
    // URL from where to get the token from
    tokenURL: 'https://api.example.com/oauth2/token',

    // optional extra default headers
    headers: {
    'x-baz': 'some value for an extra header'
    },

    // optional and additional / custom
    // axios configuration
    config: {
    // this always returns (true) by default
    validateStatus: (status) => status < 400,
    }
    })

    // now, do some API call with a pre-configured client
    // that submits the access_token as Bearer via
    // Authorization header
    await client.get('/foo?bar=baz') // [GET] https://api.example.com/foo?bar=baz

    Parameters

    Returns Promise<AxiosInstance>

    The promise with the new client.

createClientCredentialsClientFactory

  • Creates a factory function, which always creates a new pre-configured axios instance, that first gets an access token via an OAuth 2.0 workflow of type 'client_credentials'.

    example
    import createClientCredentialsClientFactory from '@egomobile/oauth2-client'

    const createClient = createClientCredentialsClientFactory({
    // data for client authorization
    auth: {
    clientId: 'foo',
    clientSecret: 'bar'
    },

    // base URL of the API
    baseURL: 'https://api.example.com',
    // URL from where to get the token from
    tokenURL: 'https://api.example.com/oauth2/token',

    // optional extra default headers
    headers: {
    'x-baz': 'some value for an extra header'
    },

    // optional and additional / custom
    // axios configuration
    config: {
    // this always returns (true) by default
    validateStatus: (status) => status < 400,
    }
    })

    function doApiCall() {
    // create client with the new factory
    // without subitting the credentials
    // every time
    const client = await createClient()

    // now, do some API call with a pre-configured client
    // that submits the access_token as Bearer via
    // Authorization header
    return client.get('/foo?bar=baz') // [GET] https://api.example.com/foo?bar=baz
    }

    const response = await doApiCall()

    Parameters

    Returns AxiosFactory

    The new factory.

createPasswordClient

  • Creates a new pre-configured axios instance, that first gets an access token via an OAuth 2.0 workflow of type 'password'.

    example
    import { createPasswordClient } from '@egomobile/oauth2-client'

    const client = await createPasswordClient({
    // data for client authorization
    auth: {
    clientId: 'foo', clientSecret: 'bar',
    username: 'bill', password: 'G@tes1234!'
    },

    // base URL of the API
    baseURL: 'https://api.example.com',
    // URL from where to get the token from
    tokenURL: 'https://api.example.com/oauth2/token',

    // optional extra default headers
    headers: {
    'x-baz': 'some value for an extra header'
    },

    // optional and additional / custom
    // axios configuration
    config: {
    // this always returns (true) by default
    validateStatus: (status) => status < 400,
    }
    })

    // now, do some API call with a pre-configured client
    // that submits the access_token as Bearer via
    // Authorization header
    await client.get('/foo?bar=baz') // [GET] https://api.example.com/foo?bar=baz

    Parameters

    Returns Promise<AxiosInstance>

    The promise with the new client.

createPasswordClientFactory

  • Creates a factory function, which always creates a new pre-configured axios instance, that first gets an access token via an OAuth 2.0 workflow of type 'password'.

    example
    import createPasswordClientFactory from '@egomobile/oauth2-client'

    const createClient = createPasswordClientFactory({
    // data for client authorization
    auth: {
    clientId: 'foo', clientSecret: 'bar',
    username: 'bill', password: 'G@tes1234!'
    },

    // base URL of the API
    baseURL: 'https://api.example.com',
    // URL from where to get the token from
    tokenURL: 'https://api.example.com/oauth2/token',

    // optional extra default headers
    headers: {
    'x-baz': 'some value for an extra header'
    },

    // optional and additional / custom
    // axios configuration
    config: {
    // this always returns (true) by default
    validateStatus: (status) => status < 400,
    }
    })

    function doApiCall() {
    // create client with the new factory
    // without subitting the credentials
    // every time
    const client = await createClient()

    // now, do some API call with a pre-configured client
    // that submits the access_token as Bearer via
    // Authorization header
    return client.get('/foo?bar=baz') // [GET] https://api.example.com/foo?bar=baz
    }

    const response = await doApiCall()

    Parameters

    Returns AxiosFactory

    The new factory.

Generated using TypeDoc