@c2c-platform/sdk-server
v1.0.6
Published
This is a C2C Platform SDK for server.
Downloads
132
Readme
C2C Platform - SDK Server
Installation
Install the package from npm repository
$ npm install @c2c-platform/sdk-server
Features
- Products Api
- Get Product info
- Update Product
- Roles Api
- Find Roles
- Get Role
- Get Role By Code
- Create Role
- Update Role
- Delete Role
- Assign Permissions To Role
- Remove Permissions From Role
- Users Api
- Find Users
- Get User
- Get User By Email
- Invite User
- Update User
- Delete User
- Assign Roles To User
- Remove Roles From User
- Archive User
- Unarchive User
- Multiple Languages
- Verify Token / Check Permission
- Verify Token
- Check Permission
Usage / Examples
Usage of SDK
import { Platform } from '@c2c-platform/sdk-server'
// Initialize the platform instance
const platform = new Platform({
baseUrl: 'https://api-admin.scrum-dev.com',
clientId: '6385aff93780bfb6a84d85b9',
clientSecret: '9290993cce5eb4ba6fedafb7894de720'
});
// use the methods to interactive with api server
await platform.product.updateProduct(...)
API Reference
Product Api
getProductInfo(options)
| Parameter | Type | Required | Description |
| :-------- | :------- | :-------: |:------------------------- |
| options
| ApiOptions
| no | |
// example
try {
const result = await platform.product.getProductInfo({ headers : { acceptLanguage: 'ja' } });
} catch (err) {
console.log(err);
}
// or
platform.product.getProductInfo({ headers : { acceptLanguage: 'ja' } }).then((result) => console.log(result));
updateProduct(data, options)
| Parameter | Type | Required | Description |
| :-------- | :------- | :-------: |:------------------------- |
| data
| IUpdateProductInput
| yes | |
| options
| ApiOptions
| no | |
// example
const data: IUpdateProductInput = {
name: 'Product Example',
logo: 'https://example.com/static/logo.svg',
domain: 'example.com',
auth0: {
tokenExpire: 3600,
refreshTokenExpire: 3600,
loginCallbackUrl: ['https://example.com/login/callback'],
logoutCallbackUrl: ['https://example.com'];
}
};
try {
const result = await platform.product.updateProduct(data, { headers : { acceptLanguage: 'ja' } });
} catch (err) {
console.log(err);
}
// or
platform.product.updateProduct(data, { headers : { acceptLanguage: 'ja' } }).then((result) => console.log(result));
Role Api
findRoles(params, options)
| Parameter | Type | Required| Description |
| :-------- | :------- | :-------: | :-------------------------------- |
| params
| IFindRoleInput
| no | |
| options
| ApiOptions
| no | |
// example
try {
const result = await platform.role.findRoles({ keyword: 'example', skip: 0, limit: 50 }, { headers : { acceptLanguage: 'ja' } });
} catch (err) {
console.log(err);
}
// or
platform.role.findRoles({ keyword: 'keyword' })
.then((result) => console.log(result))
.catch((err) => console.log(err));
getRole(id, options)
| Parameter | Type | Required| Description |
| :-------- | :------- | :-------: | :-------------------------------- |
| id
| string
| yes | Id of role |
| options
| ApiOptions
| no | Optional |
// example
try {
const result = await platform.role.getRole('{roleid}');
} catch (err) {
console.log(err);
}
//or
platform.role.getRole('{roleid}')
.then((result) => console.log(result))
.catch((err) => console.log(err));
getRoleByCode(id, options)
| Parameter | Type | Required| Description |
| :-------- | :------- | :-------: | :-------------------------------- |
| code
| string
| yes | Code of role |
| options
| ApiOptions
| no | Optional |
// example
try {
const result = await platform.role.getRoleByCode('{roleCode}');
} catch (err) {
console.log(err);
}
//or
platform.role.getRoleByCode('{roleCode}')
.then((result) => console.log(result))
.catch((err) => console.log(err));
createRole(data, options)
| Parameter | Type | Required| Description |
| :-------- | :------- | :-------: | :-------------------------------- |
| data
| ICreateRoleInput
| yes | The data for creating role |
| options
| ApiOptions
| no | |
// example
try {
const result = await platform.role.createRole({
name: 'Example',
code: 'example',
isRoot: false,
level: 2,
})
} catch (err) {
console.log(err);
}
// or
const result = await platform.role.createRole({
name: 'Example',
code: 'example',
isRoot: false,
level: 2,
}).then((result) => console.log(result)).catch((err) => console.log(err));
updateRole(id, data, options)
| Parameter | Type | Required| Description |
| :-------- | :------- | :-------: | :-------------------------------- |
| id
| string
| yes | Id of role to update |
| data
| IUpdateRoleInput
| yes | The data to update role |
| options
| ApiOptions
| no | |
try {
const result = await platform.role.updateRole('{roleid}', {
name: 'Example Revised',
code: 'example',
});
} catch (err) {
console.log(err);
}
// or
const result = await platform.role.updateRole('{roleid}', {
name: 'Example Revised',
code: 'example',
}).then((result) => console.log(result)).catch((err) => console.log(err));
deleteRole(id, options)
| Parameter | Type | Required| Description |
| :-------- | :------- | :-------: | :-------------------------------- |
| id
| string
| yes | Id of role to delete |
| options
| ApiOptions
| no | |
// example
try {
const result = await platform.role.deleteRole('{roleid}');
} catch (err) {
console.log(err);
}
// or
platform.role.deleteRole('{roleid}')
.then((result) => console.log(result))
.catch((err) => console.log(err));
assignPermissions(id, permissions, options)
| Parameter | Type | Required| Description |
| :-------- | :------- | :-------: | :-------------------------------- |
| id
| string
| yes | Id of role to assign permissions |
| permissions
| string[]
| yes | Example: ['read:user', 'update:user']
|
| options
| ApiOptions
| no | |
// example
try {
const result = await platform.role.assignPermissions('{roleid}', ['read:user', 'update:user']);
} catch (err) {
console.log(err);
}
// or
platform.role.assignPermissions('{roleid}', ['read:user', 'update:user'])
.then((result) => console.log(result))
.catch((err) => console.log(err));
unassignPermissions(id, permissions, options)
| Parameter | Type | Required| Description |
| :-------- | :------- | :-------: | :-------------------------------- |
| id
| string
| yes | Id of role to remove permissions |
| permissions
| string[]
| yes | Example: ['read:user', 'update:user']
|
| options
| ApiOptions
| no | |
// example
try {
const result = await platform.role.unassignPermissions('{roleid}', ['read:user', 'update:user']);
} catch (err) {
console.log(err);
}
// or
platform.role.unassignPermissions('{roleid}', ['read:user', 'update:user'])
.then((result) => console.log(result))
.catch((err) => console.log(err));
User Api
findUsers(params, options)
| Parameter | Type | Required| Description |
| :-------- | :------- | :-------: | :-------------------------------- |
| params
| IFindUserInput
| no | |
| options
| ApiOptions
| no | |
// example
try {
const result = await platform.user.findUsers({ keyword: 'keyword', status: 'activated', skip: 0, limit: 50 });
} catch (err) {
console.log(err);
}
// or
platform.user.findUsers({ keyword: 'keyword', status: 'activated', skip: 0, limit: 50 })
.then((result) => console.log(result))
.catch((err) => console.log(err));
getUser(id, options)
| Parameter | Type | Required| Description |
| :-------- | :------- | :-------: | :-------------------------------- |
| id
| string
| yes | Id of user to fetch |
| options
| ApiOptions
| no | |
// example
try {
const result = await platform.user.getUser('{userId}');
} catch (err) {
console.log(err);
}
// or
platform.user.getUser('{userId}')
.then((result) => console.log(result))
.catch((err) => console.log(err));
getUserByEmail(email, options)
| Parameter | Type | Required| Description |
| :-------- | :------- | :-------: | :-------------------------------- |
| email
| string
| yes | Email of user to fetch |
| options
| ApiOptions
| no | |
// example
try {
const result = await platform.user.getUserByEmail('{email}');
} catch (err) {
console.log(err);
}
// or
platform.user.getUserByEmail('{email}')
.then((result) => console.log(result))
.catch((err) => console.log(err));
inviteUser(data, options)
| Parameter | Type | Required| Description |
| :-------- | :------- | :-------: | :-------------------------------- |
| data
| IInviteUserInput
| yes | The data for invite user |
| options
| ApiOptions
| no | |
// example
try {
const result = await platform.user.inviteUser({
email: '[email protected]',
roleIds: ['roleId1', 'roleId2'],
});
} catch (err) {
console.log(err);
}
// or
platform.user.inviteUser({
email: '[email protected]',
roleIds: ['roleId1', 'roleId2'],
})
.then((result) => console.log(result))
.catch((err) => console.log(err));
updateUser(id, data, options)
| Parameter | Type | Required| Description |
| :-------- | :------- | :-------: | :-------------------------------- |
| id
| string
| yes | Id of user to update |
| data
| IUpdateUserInput
| yes | The data to update user |
| options
| ApiOptions
| no | |
// example
try {
const result = await platform.user.updateUser({
firstName: 'First name',
lastName: 'Last name',
metadata: {},
});
} catch (err) {
console.log(err);
}
// or
platform.user.updateUser({
firstName: 'First name',
lastName: 'Last name',
metadata: {},
})
.then((result) => console.log(result))
.catch((err) => console.log(err));
deleteUser(id, options)
| Parameter | Type | Required| Description |
| :-------- | :------- | :-------: | :-------------------------------- |
| id
| string
| yes | Id of user to delete. For delete user, you must change status user to archive |
| options
| ApiOptions
| no | |
// example
try {
const result = await platform.user.deleteUser('{userId}');
} catch (err) {
console.log(err);
}
// or
platform.user.deleteUser('{userId}')
.then((result) => console.log(result))
.catch((err) => console.log(err));
assignRoles(id, roleIds, options)
| Parameter | Type | Required| Description |
| :-------- | :------- | :-------: | :-------------------------------- |
| id
| string
| yes | Id of user to assign roles |
| roleIds
| string[]
| yes | Example: ['{roleId01}', '{roleId02}']
|
| options
| ApiOptions
| no | |
// example
try {
const result = await platform.user.assignRoles('{userId}', ['{roleId01}', '{roleId02}']);
} catch (err) {
console.log(err);
}
// or
platform.user.assignRoles('{userId}', ['{roleId01}', '{roleId02}'])
.then((result) => console.log(result))
.catch((err) => console.log(err));
unassignRoles(id, roleIds, options)
| Parameter | Type | Required| Description |
| :-------- | :------- | :-------: | :-------------------------------- |
| id
| string
| yes | Id of user to remove roles |
| roleIds
| string[]
| yes | Example: ['{roleId01}', '{roleId02}']
|
| options
| ApiOptions
| no | |
// example
try {
const result = await platform.user.unassignRoles('{userId}', ['{roleId01}', '{roleId02}']);
} catch (err) {
console.log(err);
}
// or
platform.user.unassignRoles('{userId}', ['{roleId01}', '{roleId02}'])
.then((result) => console.log(result))
.catch((err) => console.log(err));
archiveUser(id, options)
| Parameter | Type | Required| Description |
| :-------- | :------- | :-------: | :-------------------------------- |
| id
| string
| yes | Id of user to archive |
| options
| ApiOptions
| no | |
// example
try {
const result = await platform.user.archiveUser('{userId}');
} catch (err) {
console.log(err);
}
// or
platform.user.archiveUser('{userId}')
.then((result) => console.log(result))
.catch((err) => console.log(err));
unarchiveUser(id, options)
| Parameter | Type | Required| Description |
| :-------- | :------- | :-------: | :-------------------------------- |
| id
| string
| yes | Id of user to unarchive |
| options
| ApiOptions
| no | |
// example
try {
const result = await platform.user.unarchiveUser('{userId}');
} catch (err) {
console.log(err);
}
// or
platform.user.unarchiveUser('{userId}')
.then((result) => console.log(result))
.catch((err) => console.log(err));
Multiple Languages
You can setup the default languages by the options once initialize the platform like below
// Initialize the platform class with clientId and secretKey values
const platform = new Platform({
baseUrl: 'https://api-admin.scrum-dev.com',
clientId: '6385aff93780bfb6a84d85b9',
clientSecret: '9290993cce5eb4ba6fedafb7894de720',
defaultLanguage: 'ja'
});;
Or, set the specified language by the api option param once executing the method (this usage is higher priority)
try {
const result = await platform.product.updateProduct(data, { headers : { acceptLanguage: 'ja' } });
} catch (err) {
console.log(err);
}
Verify Token / Check Permission
These are functions for verifying and checking the permission from the Auth0 token.
verifyToken(token, secret)
| Parameter | Type | Required| Description |
| :-------- | :------- | :-------: | :-------------------------------- |
| token
| string
| yes | The Auth0 token |
| secret
| string
| yes | The Auth0 secret key |
// for example
import { verifyToken } from '@c2c-platform/sdk-server';
// verify token
const data = verifyToken('{JWT}', '{JWT_SECRET}');
console.log(data)
hasPermission(data, permission)
| Parameter | Type | Required| Description |
| :-------- | :------- | :-------: | :-------------------------------- |
| data
| IUserAuthenticated
| yes | The authenticated data after verifyToken (throw the PermissionError if the checking permission does not exist in the scopes) |
| permissions
| ...string[]
| yes | The checking permission |
// for example
import { verifyToken, hasPermission } from '@c2c-platform/sdk-server';
// verify token
const authenticatedData = verifyToken('{JWT}', '{JWT_SECRET}');
console.log(authenticatedData)
// Checking the permissions
const result = hasPermission(authenticatedData, 'update:user', 'read:user');
console.log(result)