Function createServiceClient

  • Creates a new API client instance, only for a specific service.

    Parameters

    Returns ApiClient

    The new client instance.

    Example

    import { createServiceClient } from '@egomobile/api-client'

    // setup a client with the following base URL:
    //
    // https://api.example.com/my-service/v2
    const apiClient = createServiceClient({
    clientOptions: {
    // do a client_credentials oAuth flow
    auth: {
    clientId: 'my-client-id',
    clientSecret: 'my-client-secret'
    },
    baseURL: 'https://api.example.com/'
    },
    service: 'my-service',
    version: 'v2' // this is optional and 'v1' by default
    })

    // setup a BETA client with the following base URL:
    //
    // https://api.example.com/my-2nd-service/beta
    const betaApiClient = createServiceClient({
    clientOptions: {
    auth: {
    clientId: 'my-client-id-2',
    clientSecret: 'my-client-secret-2'
    },
    baseURL: 'https://api.example.com/'
    },
    service: 'my-2nd-service',
    version: 'beta'
    })

    // do a GET request on https://api.example.com/my-service/v2/foo
    const getResponse = await apiClient.withClient(async (client) => {
    return await client.get('/foo')
    })

    // do a POST request on https://api.example.com/my-service/v2/bar
    const postResponse = await apiClient.withClient(async (client) => {
    return await client.post('/bar', {
    baz: 42
    })
    })

    // do a DELETE request on https://api.example.com/my-2nd-service/beta/baz-resource/42
    const deleteResponse = await betaApiClient.withClient(async (client) => {
    return await client.delete("/baz-resource/42")
    })

Generated using TypeDoc