@gigya-ts/gigya
TypeScript icon, indicating that this package has built-in type declarations

0.1.7 • Public • Published

@gigya-ts/gigya

A tiny type-safe wrapper client for interacting with the Gigya REST API, for Node.js and browsers.

See @gigya-ts/web-sdk if you are working with the Gigya Web SDK instead.

Installation & Usage

Install the @gigya-ts/gigya package from your package manager of choice:

# npm
npm add @gigya-ts/gigya
# yarn
yarn add @gigya-ts/gigya
# pnpm
pnpm add @gigya-ts/gigya

0. (Optional) Define your Gigya schemas

When defining your Gigya schemas, it is recommended to make all fields optional. Gigya will only return fields that have been set, and even required fields may not be there (e.g. if a user has not consented to a required consent yet).

Keeping all fields optional in type definitions force will you to handle cases where a field is not set in Gigya, which is a good practice to avoid runtime errors.

import { type GigyaPreference } from '@gigya-ts/gigya';

/**
 * Your data schema.
 */
type MyDataSchema = {
    myDataSchemaString?: string;
    myDataSchemaObject?: {
        myDataSchemaNumber?: number;
    };
};

/**
 * Your preferences schema, use 'GigyaPreference' to type preferences.
 */
type MyPreferencesSchema = {
    terms?: {
        myTermsSchema?: GigyaPreference;
    };
    myPreferencesSchema?: GigyaPreference;
};

/**
 * Your subscriptions schema.
 */
type MySubscriptionsSchema = {};

1. Create a Gigya client and choose an authentication method

Create a new Gigya client instance with your API key and data center. Make sure to pass your own schemas as generics if you want the client to return your own types.

import { Gigya } from '@gigya-ts/gigya';

const gigya = Gigya<MyDataSchema, MyPreferencesSchema, MySubscriptionsSchema>({
    apiKey: '4_qd71...',
    dataCenter: 'eu1.gigya.com',
    // see credentials in next step
});

Then add your authentication credentials. @gigya-ts/gigya supports multiple authentication methods:

(Recommended) User/Application key and private key

const gigya = Gigya({
    apiKey: '4_qd71...',
    dataCenter: 'eu1.gigya.com',
    credentials: {
        type: 'asymmetric-key',
        userKey: process.env.GIGYA_USER_KEY,
        privateKey: process.env.GIGYA_PRIVATE_KEY,
    },
});

User/Application key and secret

const gigya = Gigya({
    apiKey: '4_qd71...',
    dataCenter: 'eu1.gigya.com',
    credentials: {
        type: 'key-secret',
        userKey: process.env.GIGYA_USER_KEY,
        secret: process.env.GIGYA_USER_SECRET,
    },
});

No authentication

const gigya = Gigya({
    apiKey: '4_qd71...',
    dataCenter: 'eu1.gigya.com',
    credentials: { type: 'none' },
});

3. Make a request to the Gigya REST API

// Call the "accounts.setAccountInfo" API method from the Gigya API
const getAccountInfoResponse = await gigya.accounts.setAccountInfo({
    UID: 'YOUR_UID',
    profile: {
        lastName: 'Doe',
    },
});

// Check we got a successful response
if (getAccountInfoResponse.errorCode !== 0) throw new Error(getAccountInfoResponse.errorDetails);

Readme

Keywords

none

Package Sidebar

Install

npm i @gigya-ts/gigya

Weekly Downloads

82

Version

0.1.7

License

none

Unpacked Size

19.1 kB

Total Files

8

Last publish

Collaborators

  • tswymer