@gigya-ts/gigya
v0.1.13
Published
Type-safe Gigya client for the browser and Node.js
Downloads
659
Readme
@gigya-ts/gigya
A tiny type-safe client for interacting with the Gigya REST API for Node.js and the browser.
See @gigya-ts/web-sdk if you are using the Gigya Web SDK instead.
Installation
Install the @gigya-ts/gigya
package from your package manager of choice:
# npm
npm add @gigya-ts/gigya
# pnpm
pnpm add @gigya-ts/gigya
Usage
0. (Optional) Define your Custom Gigya Schemas
See adding custom schemas to define your custom schemas (data, preferences, etc.) so that response types from the REST API include your custom fields.
1. Create a Gigya Client
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
});
2. Choose an Authentication Method
@gigya-ts/gigya
supports multiple different authentication methods:
(Recommended) User/Application key and private key
Creates a short-lived bearer token for every request using admin or application user's RSA key-pair. This is the recommended approach when accessing the REST API with server credentials as real privateKey is never actually sent accross the wire itself. See Asymmetric Keys for more information.
export 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
Attaches a userKey and secret from an admin or application user to every request made to the REST API. See Application and User Keys for more information.
export 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,
},
});
Bearer Token
Uses an accessToken obtained from socialize.getToken to call the REST API using the authorizations of a single user (even without passing their UID). Only supports the grant_type=client_credentials
or grant_type=none
flows.
export const gigya = Gigya({
apiKey: '4_qd71...',
dataCenter: 'eu1.gigya.com',
credentials: {
type: 'bearer-token',
// Obtained from socialize.getToken
token: 'st2.s.AtLtNX...',
},
});
No Authentication
Make unauthenticated requests to the REST API without any credentials (e.g. to fetch public schemas, policies, ...).
export const gigya = Gigya({
apiKey: '4_qd71...',
dataCenter: 'eu1.gigya.com',
credentials: { type: 'none' },
});
3. Make a Request to the Gigya REST API
Once Gigya
has been configured, make requests to the REST API using the created client:
// Call the "accounts.setAccountInfo" API method from the Gigya API
const setAccountInfoResponse = await gigya.accounts.setAccountInfo({
UID: 'YOUR_UID',
profile: {
lastName: 'Doe',
},
});
// Check we got a successful response
if (setAccountInfoResponse.errorCode !== 0) throw new Error(getAccountInfoResponse.errorMessage);
Examples
See examples/gigya for more examples.