ciam-client
Advanced tools
Comparing version 1.4.0 to 1.5.0
import { Axios } from 'axios'; | ||
import { Model } from 'ciam-commons'; | ||
declare type User = Model.User; | ||
declare type DiscordUser = Model.DiscordUser; | ||
declare type Role = Model.Role; | ||
declare type Permission = Model.Permission; | ||
import { CheckResult, DiscordUser, Permission, Role, User } from 'ciam-commons'; | ||
declare class Ciam { | ||
token: string; | ||
baseUrl: string; | ||
domain: string; | ||
api: Axios; | ||
constructor(token: string, baseUrl: string); | ||
self: User; | ||
constructor(token: string, baseUrl: string, domain: string); | ||
setToken(token: string): Promise<undefined>; | ||
@@ -18,3 +16,3 @@ /** | ||
* @param roleId the id of the role to get | ||
* @returns A {@link Model.Role}, or undefined | ||
* @returns A {@link Role}, or undefined | ||
* @throws if {@link roleId} is not a valid objectId | ||
@@ -28,3 +26,3 @@ */ | ||
* @param roleId the id of the role to delete | ||
* @returns the deleted {@link Model.Role}, or undefined | ||
* @returns the deleted {@link Role}, or undefined | ||
* @throws if {@link roleId} is not a valid objectId | ||
@@ -40,3 +38,3 @@ */ | ||
* @param permissions the permissions of this role | ||
* @returns the new {@link Model.Role}, or undefined | ||
* @returns the new {@link Role}, or undefined | ||
* @throws if {@link name} is empty | ||
@@ -52,3 +50,3 @@ * @throws if {@link description} is empty | ||
* @param role the role to update, with its fields modified to the desired values. Empty fields remain unchanged | ||
* @returns the updated {@link Model.Role}, or undefined | ||
* @returns the updated {@link Role}, or undefined | ||
* @throws if {@link role}._id is not a valid objectId | ||
@@ -66,3 +64,3 @@ * @throws if {@link role}.name exists and is empty | ||
* @param limit maximum number of roles to return in one request | ||
* @returns an array of {@link Model.Role}, or undefined | ||
* @returns an array of {@link Role}, or undefined | ||
* @throws if {@link skip} is less than 0 | ||
@@ -80,3 +78,3 @@ * @throws if {@link limit} is not in the range 1..100 | ||
* @param discord the discord object of this user | ||
* @returns the newly created {@link Model.User}, or unefined | ||
* @returns the newly created {@link User}, or unefined | ||
* @throws if {@link name} is empty | ||
@@ -93,3 +91,3 @@ * @throws if {@link discord}.id is not a valid discord id | ||
* @param userId the id of the user to get | ||
* @returns the {@link Model.User}, or undefined | ||
* @returns the {@link User}, or undefined | ||
* @throws if {@link userId} is not a valid objectId | ||
@@ -103,6 +101,6 @@ */ | ||
* @param userId id of the user to delete | ||
* @returns the deleted {@link Model.User}, or undefined | ||
* @returns the deleted {@link User}, or undefined | ||
* @throws if {@link userId} is not a valid objectId | ||
*/ | ||
deleteUser(userId: string): Promise<Model.Role | undefined>; | ||
deleteUser(userId: string): Promise<Role | undefined>; | ||
/** | ||
@@ -113,3 +111,3 @@ * Update a user | ||
* @param user the user to update, with their fields modified to the desired values. Empty fields remain unchanged | ||
* @returns the updated {@link Model.User}, or undefined | ||
* @returns the updated {@link User}, or undefined | ||
* @throws if {@link user}._id is not a valid objectId | ||
@@ -128,3 +126,3 @@ * @throws if {@link user}.name exists and is empty | ||
* @param limit maximum number of users to return in one request | ||
* @returns an array of {@link Model.Role}, or undefined | ||
* @returns an array of {@link Role}, or undefined | ||
* @throws if {@link skip} is less than 0 | ||
@@ -145,3 +143,3 @@ * @throws if {@link limit} is not in the range 1..100 | ||
* @param flag the flag to get permissions for | ||
* @returns the {@link Model.Permission} for this flag, or undefined | ||
* @returns the {@link Permission} for this flag, or undefined | ||
* @throws if {@link flag} is not a valid strict flag | ||
@@ -155,3 +153,3 @@ */ | ||
* @param flag the flag to delete | ||
* @returns the deleted {@link Model.Permission}, or undefined | ||
* @returns the deleted {@link Permission}, or undefined | ||
* @throws if {@link flag} is not a valid strict flag | ||
@@ -167,3 +165,3 @@ */ | ||
* @param flag the flag of this permission | ||
* @returns the new {@link Model.Permission}, or undefined | ||
* @returns the new {@link Permission}, or undefined | ||
* @throws if {@link name} is empty | ||
@@ -179,3 +177,3 @@ * @throws if {@link description} is empty | ||
* @param permission the permission to update, with its fields modified to the desired values. Empty fields remain unchanged | ||
* @returns the updated {@link Model.Permission}, or undefined | ||
* @returns the updated {@link Permission}, or undefined | ||
* @throws if {@link permission}.flag is not a valid strict flag | ||
@@ -192,3 +190,3 @@ * @throws if {@link permission}.name exists and is empty | ||
* @param limit maximum number of permissions to return | ||
* @returns an array of {@link Model.Permission}, or undefined | ||
* @returns an array of {@link Permission}, or undefined | ||
* @throws if {@link skip} is less than 0 | ||
@@ -204,3 +202,3 @@ * @throws if {@link limit} is not in the range 1..100 | ||
* @param limit maximum number of permissions to return | ||
* @returns an array of {@link Model.Permission}, or undefined | ||
* @returns an array of {@link Permission}, or undefined | ||
* @throws if {@link skip} is less than 0 | ||
@@ -219,3 +217,3 @@ * @throws if {@link limit} is not in the range 1..100 | ||
* @param includeMissing if the response should include the missing permissions | ||
* @returns a {@link Model.CheckResult}, or undefined | ||
* @returns a {@link CheckResult}, or undefined | ||
* @throws if {@link type} is not one of `user`, `role`, or `discordUser` | ||
@@ -227,4 +225,6 @@ * @throws if {@link type} is `user` or `role` and {@link id} is not a valid objectId | ||
*/ | ||
checkPermissions(type: 'user' | 'role' | 'discordUser', id: string, required: Array<string>, additional?: Array<string>, includeMissing?: boolean): Promise<Model.CheckResult | undefined>; | ||
checkPermissions(type: 'user' | 'role' | 'discordUser', id: string, required: Array<string>, additional?: Array<string>, includeMissing?: boolean): Promise<CheckResult | undefined>; | ||
generateAuthUrl(returnUrl: string): string; | ||
getUserFromAuthToken(authToken: string): Promise<User | undefined>; | ||
} | ||
export { Ciam }; |
139
index.js
import axios from 'axios'; | ||
import { Check } from 'ciam-commons'; | ||
import { discordId, flag, inRange, min, notEmpty, objectId, oneOf, strictFlag } from 'ciam-commons'; | ||
class InvalidToken extends Error { | ||
@@ -21,6 +21,9 @@ constructor(token) { | ||
baseUrl; | ||
domain; | ||
api; | ||
constructor(token, baseUrl) { | ||
self; | ||
constructor(token, baseUrl, domain) { | ||
this.token = token; | ||
this.baseUrl = baseUrl; | ||
this.domain = domain; | ||
this.api = axios; | ||
@@ -47,5 +50,7 @@ this.setToken(token); | ||
}); | ||
const validRes = await this.api.get('/user/valid'); | ||
const validRes = await this.api.get('/user/me'); | ||
if (validRes.status != 200) | ||
return Promise.reject(new InvalidToken(token)); | ||
else | ||
this.self = await validRes.data; | ||
} | ||
@@ -57,7 +62,7 @@ /** | ||
* @param roleId the id of the role to get | ||
* @returns A {@link Model.Role}, or undefined | ||
* @returns A {@link Role}, or undefined | ||
* @throws if {@link roleId} is not a valid objectId | ||
*/ | ||
async getRole(roleId) { | ||
Check.objectId(roleId); | ||
objectId(roleId); | ||
return cast(await this.api.get(`/role/${roleId}`)); | ||
@@ -70,7 +75,7 @@ } | ||
* @param roleId the id of the role to delete | ||
* @returns the deleted {@link Model.Role}, or undefined | ||
* @returns the deleted {@link Role}, or undefined | ||
* @throws if {@link roleId} is not a valid objectId | ||
*/ | ||
async deleteRole(roleId) { | ||
Check.objectId(roleId); | ||
objectId(roleId); | ||
return cast(await this.api.delete(`/role/${roleId}`)); | ||
@@ -85,3 +90,3 @@ } | ||
* @param permissions the permissions of this role | ||
* @returns the new {@link Model.Role}, or undefined | ||
* @returns the new {@link Role}, or undefined | ||
* @throws if {@link name} is empty | ||
@@ -92,5 +97,5 @@ * @throws if {@link description} is empty | ||
async createRole(name, description, permissions) { | ||
Check.notEmpty(name, 'name'); | ||
Check.notEmpty(description, 'description'); | ||
permissions.forEach(f => Check.flag(f)); | ||
notEmpty(name, 'name'); | ||
notEmpty(description, 'description'); | ||
permissions.forEach(f => flag(f)); | ||
const obj = { | ||
@@ -108,3 +113,3 @@ name: name, | ||
* @param role the role to update, with its fields modified to the desired values. Empty fields remain unchanged | ||
* @returns the updated {@link Model.Role}, or undefined | ||
* @returns the updated {@link Role}, or undefined | ||
* @throws if {@link role}._id is not a valid objectId | ||
@@ -116,8 +121,8 @@ * @throws if {@link role}.name exists and is empty | ||
async updateRole(role) { | ||
Check.objectId(role._id); | ||
role.permissions?.forEach(p => Check.flag(p)); | ||
objectId(role._id); | ||
role.permissions?.forEach(p => flag(p)); | ||
if (role.name) | ||
Check.notEmpty(role.name, 'role.name'); | ||
notEmpty(role.name, 'role.name'); | ||
if (role.description) | ||
Check.notEmpty(role.name, 'role.description'); | ||
notEmpty(role.name, 'role.description'); | ||
return cast(await this.api.post('/role/update', role)); | ||
@@ -131,3 +136,3 @@ } | ||
* @param limit maximum number of roles to return in one request | ||
* @returns an array of {@link Model.Role}, or undefined | ||
* @returns an array of {@link Role}, or undefined | ||
* @throws if {@link skip} is less than 0 | ||
@@ -137,4 +142,4 @@ * @throws if {@link limit} is not in the range 1..100 | ||
async listRoles(skip = 0, limit = 100) { | ||
Check.min(skip, 0, 'skip'); | ||
Check.inRange(limit, 1, 100, 'limit'); | ||
min(skip, 0, 'skip'); | ||
inRange(limit, 1, 100, 'limit'); | ||
return cast(await this.api.get(`/role/list?skip=${skip}&limit=${limit}`)); | ||
@@ -150,3 +155,3 @@ } | ||
* @param discord the discord object of this user | ||
* @returns the newly created {@link Model.User}, or unefined | ||
* @returns the newly created {@link User}, or unefined | ||
* @throws if {@link name} is empty | ||
@@ -158,7 +163,7 @@ * @throws if {@link discord}.id is not a valid discord id | ||
async createUser(name, roles, permissions, discord) { | ||
Check.notEmpty(name, 'name'); | ||
notEmpty(name, 'name'); | ||
if (discord) | ||
Check.discordId(discord.id, 'discord.id'); | ||
roles.forEach(r => Check.objectId(r)); | ||
permissions.forEach(p => Check.flag(p)); | ||
discordId(discord.id, 'discord.id'); | ||
roles.forEach(r => objectId(r)); | ||
permissions.forEach(p => flag(p)); | ||
const obj = { | ||
@@ -177,7 +182,7 @@ name: name, | ||
* @param userId the id of the user to get | ||
* @returns the {@link Model.User}, or undefined | ||
* @returns the {@link User}, or undefined | ||
* @throws if {@link userId} is not a valid objectId | ||
*/ | ||
async getUser(userId) { | ||
Check.objectId(userId); | ||
objectId(userId); | ||
return cast(await this.api.get(`/user/${userId}`)); | ||
@@ -190,7 +195,7 @@ } | ||
* @param userId id of the user to delete | ||
* @returns the deleted {@link Model.User}, or undefined | ||
* @returns the deleted {@link User}, or undefined | ||
* @throws if {@link userId} is not a valid objectId | ||
*/ | ||
async deleteUser(userId) { | ||
Check.objectId(userId); | ||
objectId(userId); | ||
return cast(await this.api.delete(`/role/${userId}`)); | ||
@@ -203,3 +208,3 @@ } | ||
* @param user the user to update, with their fields modified to the desired values. Empty fields remain unchanged | ||
* @returns the updated {@link Model.User}, or undefined | ||
* @returns the updated {@link User}, or undefined | ||
* @throws if {@link user}._id is not a valid objectId | ||
@@ -212,9 +217,9 @@ * @throws if {@link user}.name exists and is empty | ||
async updateUser(user) { | ||
Check.objectId(user._id); | ||
objectId(user._id); | ||
if (user.discord) | ||
Check.discordId(user.discord?.id, 'discord.id'); | ||
user.roles?.forEach(r => Check.objectId(r)); | ||
user.permissions?.forEach(p => Check.flag(p)); | ||
discordId(user.discord?.id, 'discord.id'); | ||
user.roles?.forEach(r => objectId(r)); | ||
user.permissions?.forEach(p => flag(p)); | ||
if (user.name) | ||
Check.notEmpty(user.name, 'user.name'); | ||
notEmpty(user.name, 'user.name'); | ||
return cast(await this.api.post('/user/update')); | ||
@@ -228,3 +233,3 @@ } | ||
* @param limit maximum number of users to return in one request | ||
* @returns an array of {@link Model.Role}, or undefined | ||
* @returns an array of {@link Role}, or undefined | ||
* @throws if {@link skip} is less than 0 | ||
@@ -234,4 +239,4 @@ * @throws if {@link limit} is not in the range 1..100 | ||
async listUsers(skip = 0, limit = 100) { | ||
Check.min(skip, 0, 'skip'); | ||
Check.inRange(limit, 1, 100, 'limit'); | ||
min(skip, 0, 'skip'); | ||
inRange(limit, 1, 100, 'limit'); | ||
return cast(await this.api.get(`/user/list?skip=${skip}&limit=${limit}`)); | ||
@@ -254,7 +259,7 @@ } | ||
* @param flag the flag to get permissions for | ||
* @returns the {@link Model.Permission} for this flag, or undefined | ||
* @returns the {@link Permission} for this flag, or undefined | ||
* @throws if {@link flag} is not a valid strict flag | ||
*/ | ||
async getPermission(flag) { | ||
Check.strictFlag(flag); | ||
strictFlag(flag); | ||
return cast(await this.api.get(`/permission/${flag}`)); | ||
@@ -267,7 +272,7 @@ } | ||
* @param flag the flag to delete | ||
* @returns the deleted {@link Model.Permission}, or undefined | ||
* @returns the deleted {@link Permission}, or undefined | ||
* @throws if {@link flag} is not a valid strict flag | ||
*/ | ||
async deletePermission(flag) { | ||
Check.strictFlag(flag); | ||
strictFlag(flag); | ||
return cast(await this.api.delete(`/permission/${flag}`)); | ||
@@ -282,3 +287,3 @@ } | ||
* @param flag the flag of this permission | ||
* @returns the new {@link Model.Permission}, or undefined | ||
* @returns the new {@link Permission}, or undefined | ||
* @throws if {@link name} is empty | ||
@@ -289,5 +294,5 @@ * @throws if {@link description} is empty | ||
async createPermission(name, description, flag) { | ||
Check.notEmpty(name, 'name'); | ||
Check.notEmpty(description, 'description'); | ||
Check.strictFlag(flag); | ||
notEmpty(name, 'name'); | ||
notEmpty(description, 'description'); | ||
strictFlag(flag); | ||
const obj = { | ||
@@ -305,3 +310,3 @@ name: name, | ||
* @param permission the permission to update, with its fields modified to the desired values. Empty fields remain unchanged | ||
* @returns the updated {@link Model.Permission}, or undefined | ||
* @returns the updated {@link Permission}, or undefined | ||
* @throws if {@link permission}.flag is not a valid strict flag | ||
@@ -312,7 +317,7 @@ * @throws if {@link permission}.name exists and is empty | ||
async updatePermission(permission) { | ||
Check.strictFlag(permission.flag); | ||
strictFlag(permission.flag); | ||
if (permission.name) | ||
Check.notEmpty(permission.name, 'permission.description'); | ||
notEmpty(permission.name, 'permission.description'); | ||
if (permission.description) | ||
Check.notEmpty(permission.description, 'permission.description'); | ||
notEmpty(permission.description, 'permission.description'); | ||
return cast(await this.api.post('/permission/update', permission)); | ||
@@ -326,3 +331,3 @@ } | ||
* @param limit maximum number of permissions to return | ||
* @returns an array of {@link Model.Permission}, or undefined | ||
* @returns an array of {@link Permission}, or undefined | ||
* @throws if {@link skip} is less than 0 | ||
@@ -332,4 +337,4 @@ * @throws if {@link limit} is not in the range 1..100 | ||
async ownPermissions(skip = 0, limit = 100) { | ||
Check.min(skip, 0, 'skip'); | ||
Check.inRange(limit, 1, 100, 'limit'); | ||
min(skip, 0, 'skip'); | ||
inRange(limit, 1, 100, 'limit'); | ||
return cast(await this.api.get(`/permission/me?skip=${skip}&limit=${limit}`)); | ||
@@ -343,3 +348,3 @@ } | ||
* @param limit maximum number of permissions to return | ||
* @returns an array of {@link Model.Permission}, or undefined | ||
* @returns an array of {@link Permission}, or undefined | ||
* @throws if {@link skip} is less than 0 | ||
@@ -349,4 +354,4 @@ * @throws if {@link limit} is not in the range 1..100 | ||
async listPermissions(skip = 0, limit = 100) { | ||
Check.min(skip, 0, 'skip'); | ||
Check.inRange(limit, 1, 100, 'limit'); | ||
min(skip, 0, 'skip'); | ||
inRange(limit, 1, 100, 'limit'); | ||
return cast(await this.api.get(`/permission/list?skip=${skip}&limit=${limit}`)); | ||
@@ -363,3 +368,3 @@ } | ||
* @param includeMissing if the response should include the missing permissions | ||
* @returns a {@link Model.CheckResult}, or undefined | ||
* @returns a {@link CheckResult}, or undefined | ||
* @throws if {@link type} is not one of `user`, `role`, or `discordUser` | ||
@@ -372,10 +377,10 @@ * @throws if {@link type} is `user` or `role` and {@link id} is not a valid objectId | ||
async checkPermissions(type, id, required, additional = [], includeMissing = false) { | ||
Check.oneOf(type, ['user', 'role', 'discordUser'], 'type'); | ||
oneOf(type, ['user', 'role', 'discordUser'], 'type'); | ||
if (type == 'user' || type == 'role') | ||
Check.objectId(id, 'id'); | ||
objectId(id, 'id'); | ||
else if (type == 'discordUser') | ||
Check.discordId(id); | ||
Check.notEmpty(required, 'required'); | ||
required.forEach(f => Check.flag(f)); | ||
additional?.forEach(f => Check.flag(f)); | ||
discordId(id); | ||
notEmpty(required, 'required'); | ||
required.forEach(f => flag(f)); | ||
additional?.forEach(f => flag(f)); | ||
return cast(await this.api.post('/permission/has', { | ||
@@ -389,3 +394,13 @@ type: type, | ||
} | ||
generateAuthUrl(returnUrl) { | ||
return `${this.baseUrl}/login?domain=${this.domain}&returnUrl=${returnUrl}&clientId=${this.self._id}&scope=identify`; | ||
} | ||
async getUserFromAuthToken(authToken) { | ||
return cast(await this.api.get('/auth/user', { | ||
headers: { | ||
'ciam_token': authToken | ||
} | ||
})); | ||
} | ||
} | ||
export { Ciam }; |
{ | ||
"name": "ciam-client", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "type": "module", |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
23847
587
0