npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@sinch/sdk-client

v1.2.0

Published

Core services related to interacting with Sinch API

Downloads

2,412

Readme

Sinch Http Client for Node.js

This package contains the HTTP client used by the Sinch SDK client. It uses 2 third-party dependencies:

  • node-fetch to send requests.
  • form-data to manage "multipart/form-data" streams.

There are 5 folders in this package:

  • api: it defines the interface for an Api, an ApiClient and for ApiClientOptions.
  • client: it contains the fetch implementation for the ApiClient. This implementation handles the refresh of the JWT for the APIs which support this authentication method.
  • domain: it contains some Sinch-specific interfaces and enums used at domain level for authentication and region definition.
  • plugins: calling an API can be seen as a straighforward operation as it's "just" about sending an HTTP request to a server and reading the response. And this is was the ApiClient does. However, all APIs don't behave the same and this is why we have introduced the request and response plugins:
    • request plugins will modify the request: for the moment, they are all about adding some headers (for authentication, for tracking the SDK usage).
    • response plugins will modify the response, e.g.: response content is checked and transformed into an exception if the content is empty.
  • utils: it contains some utility methods that are used by the API SDKs, such as the methods to validate headers are valid in a callback event request, or a method to convert text to hexadecimal for a UDH header

Architecture

High level architecture

Installation

With NPM

npm install @sinch/sdk-client

With Yarn

yarn add @sinch/sdk-client

Usage

In this section, we'll see how to implement the Api interface and how to use the ApiFetchClient to call the API endpoint.

Api interface

export class MyExampleApi implements Api {
    public readonly apiName = 'MyExampleApi';
    public readonly client: ApiClient;

    constructor(apiClient: ApiClient) {
        this.client = apiClient;
    }

    public async myApiOperation(): Promise<Response> {
        // Define the API endpoint
        const url = `${this.client.apiClientOptions.basePath}/myDomain`;
        // Build the request options: you can use the method 'prepareOptions' from the apiClient 
        // that will apply the request plugins onto the default built options
        const requestOptions: RequestOptions = await this.client.prepareOptions(...);
        // Send the request and apply the response plugins
        return this.client.processCall<Response>({
            url,
            requestOptions,
            apiName: this.apiName,
        });
    }
}

ApiFetchClient instantiation

const apiClientOptions: ApiClientOptions = {
  basePath: 'https://my-example-api-server.com',
  requestPlugins: [
    // Add here the plugins that will be executed before the request is sent
  ],
  responsePlugins: [
    // Add here the plugins that will be executed after the response is received and parsed
  ]
}

// Instantiate the API Client
const apiClient = new ApiFetchClient(apiClientOptions);

API class instantiation

// Once the API Client is intantiated, it can be given as a parameter to your API
const myApi = new MyExampleApi(apiClient);

// The API operations can now be invoked
const myResponse = await myApi.myApiOperation();

Plugins

Request plugins

| Plugin name | Description | |----------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | additional-headers | Adds multiple headers to a request. | | api-token | Used for the SMS API with servicePlanId. It adds an Authorization header with the value of the API token as a Bearer. | | basic-authentication | Alternative to the oauth2 plugin: less secure and to be used for prototyping only. It adds an Authorization header to the request to perform a Basic authentication. | | oauth2 | Used by all the APIs that supports the projectId authentication scheme. If no token is available, it will make a query to the Sinch authentication server to get a new one, valid for 1 hour. Once a token is available, it will add a Authorization header to the request with the value fo the token as Bearer. | | signing | Used by the Verification and Voice APIs: it adds an Authorization header containing the signature of the request. | | version | Add a user-agent header containing information such as the SDK version and the Node.js process version.Added by default to the ApiFetchClient | | x-timestamp | Add the current date in ISO format in the x-timesstamp header (used by the signing plugin). |

Response plugins

| Plugin name | Description | |-------------|-----------------------------------------------------------------------------------------------------------------------------------------------| | exception | Check the response content and converts it into an error if empty or if the status is not ok.Added by default to the ApiFetchClient | | timezone | Patch some response properties which are identified as returned without a timezone by the server |

Contact

Developer Experience team: [email protected]