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.
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
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 = {};
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:
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,
},
});
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,
},
});
const gigya = Gigya({
apiKey: '4_qd71...',
dataCenter: 'eu1.gigya.com',
credentials: { type: 'none' },
});
// 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);