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

@gravity-ui/sdk

v1.5.1

Published

SDK implementing work with REST/GRPC APIs

Downloads

2,603

Readme

@gravity-ui/sdk · npm package CI

SDK implementing work with REST/GRPC APIs

Install

npm install --save-dev @gravity-ui/sdk

Usage

You need to import sdkFactory and Schema, and create an instance of the class.

If the schema is not divided into scopes:

import sdkFactory from '@gravity-ui/sdk';
import type Schema from '<schemas package>';

const config = {
  csrfToken: 'secret-token',
  endpoint: '/gateway',
};

const sdk = sdkFactory<{root: typeof Schema}>(config);

If the schema is divided into scopes:

import sdkFactory from '@gravity-ui/sdk';
import type Schema from '<schemas package>';
import type LocalSchema from '../../shared/schemas';

const config = {
  csrfToken: 'secret-token',
  endpoint: '/gateway',
};

const sdk = sdkFactory<{root: typeof Schema; local: typeof LocalSchema}>(config);

Structure of config:

import {AxiosRequestConfig} from 'axios';

interface SdkConfig {
  // Custom Axios settings
  axiosConfig?: AxiosRequestConfig;
  // CSRF-token
  csrfToken?: string;
  // The endpoint to which the request from the client will be sent. By default, "/api" is used
  endpoint?: string;
  // Custom error handler. If the configuration is not specified, the default one is used
  handleRequestError?: (error: unknown) => any;
}

In the code, the invocation of the SDK method looks like this:

sdk.<scope>.<service>.<action>(data, options); // => returns a CancelablePromise

If the default endpoint value /api is specified, the full request path will look like this:

/api/:scope/:service/:action

There is a special scope called root. Its methods can be called without explicitly specifying the scope.

The following calls are equivalent:

sdk.root.<service>.<action>(data, options);

sdk.<service>.<action>(data, options);

But the full request path will always include the scope:

/api/root/:service/:action

Structure of options:

interface SdkOptions {
  // Request identifier. The previous request with the same identifier will be canceled
  concurrentId?: string;
  // Ability to specify a specific number of retries for certain endpoints. By default, the number of retries is 0
  retries?: number;
  // Ability to specify a specific timeout for certain endpoints.
  // By default, the configuration is taken from axiosConfig or set to 60 seconds
  timeout?: number;
  // Ability to specify specific headers when making a call
  headers?: Record<string, unknown>;
}

Invoking External Methods

Through the sdk, you have the ability to make calls to external methods that are not defined in the schemas. Such a call looks like this:

sdk(config, options);

The argument config has the type AxiosRequestConfig, and the argument options has the type SdkOptions described above.

CancellablePromise

For the convenience of creating cancelable promises, the package exports a class called CancellablePromise(promise: Promise, cancel: () => void).

Additional methods

sdk.setLang()

Allows setting the user's language, which will be passed to all invoked endpoints through a special accept-language header.

sdk.setDefaultHeader()

Allows setting base headers that will be passed to all invoked endpoints.