Skip to content

Commit

Permalink
add listDomainsUsers fx
Browse files Browse the repository at this point in the history
Signed-off-by: Musilah <[email protected]>
  • Loading branch information
Musilah committed Apr 4, 2024
1 parent 9167310 commit 55fc80d
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 26 deletions.
17 changes: 15 additions & 2 deletions examples/domains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import SDK from '../src/sdk'
const defaultUrl = 'http://localhost'

const mySdk = new SDK({
domainsUrl: defaultUrl + ':8189'
domainsUrl: defaultUrl + ':8189',
usersUrl: defaultUrl + ':9002'
})

mySdk.domains
.CreateDomain(
{ name: '<domainName>', email: '<domainEmail>' },
{ name: '<domainName>' },
'<token>'
)
.then((response: any) => {
Expand Down Expand Up @@ -74,6 +75,18 @@ mySdk.domains.ListUserDomains(
console.log(error)
})

mySdk.domains.ListDomainUsers(
'<domainID>',
{ offset: 0, limit: 10 },
'<token>'
)
.then((response: any) => {
console.log('response: ', response)
})
.catch((error) => {
console.log(error)
})

mySdk.domains.EnableDomain(
'<domainID>',
'<token>'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"files": [
"dist",
"sdk.js",
"mainflux",
"src",
"LICENSE"
],
"scripts": {
Expand Down
12 changes: 10 additions & 2 deletions src/defs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface User {
role?: string
status?: 'enabled' | 'disabled'
metadata?: Record<string, any>
updatedBy?: string
updated_by?: string
}

export interface UsersPage {
Expand Down Expand Up @@ -117,7 +117,15 @@ export interface Domain {
name?: string
id?: string
alias?: string
email?: string
metadata?: Record<string, any>
tags?: string[]
status?: 'enabled' | 'disabled'
permission?: string
permissions?: string[]
created_by?: string
updated_by?: string
created_at?: Date
updated_at?: Date
}

export interface DomainsPage {
Expand Down
146 changes: 126 additions & 20 deletions src/domains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,46 @@ import {
type QueryParams,
type DomainsPage,
type Permissions,
type Response
type Response,
type UsersPage
} from './defs'

export default class Domains {
// Domains API client

private readonly domainsUrl: URL
private readonly usersUrl?: URL
private readonly contentType: string
private readonly domainsEndpoint: string
private readonly domainError: Errors

public constructor (domainsUrl: string) {
public constructor ({ domainsUrl, usersUrl }: { domainsUrl: string, usersUrl?: string }) {
this.domainsUrl = new URL(domainsUrl)
if (usersUrl !== undefined) {
this.usersUrl = new URL(usersUrl)
} else {
this.usersUrl = new URL('')
}
this.contentType = 'application/json'
this.domainsEndpoint = 'domains'
this.domainError = new Errors()
}

public async CreateDomain (domain: Domain, token: string): Promise<Domain> {
// CreateDomain creates a new domain.

/**
* @method CreateDomain - Creates a new domain.
* @param {object} domain - Domain object.
* @param {string} token - User token with.
* @returns {object} - returns an object Domain
* @example
* const domain = {
* name: "domainName",
* alias: "domainAlias",
* }
*
*/
const options: RequestInit = {
method: 'POST',
headers: {
Expand Down Expand Up @@ -51,6 +71,13 @@ export default class Domains {

public async UpdateDomain (domain: Domain, token: string): Promise<Domain> {
// UpdateDomain updates an existing domain.

/**
* @method UpdateDomain - Updates an existing domain's name and metadata.
* @param {object} domain - Domain object.
* @param {string} token - User token with domain access.
* @returns {object} - returns an object Domain.
*/
const options: RequestInit = {
method: 'PATCH',
headers: {
Expand Down Expand Up @@ -78,6 +105,13 @@ export default class Domains {

public async Domain (domainID: string, token: string): Promise<Domain> {
// Domain retrieves domain with provided ID.

/**
* @method Domain - retrieves domain with provided ID.
* @param {string} domainID - domain ID.
* @param {string} token - user token.
* @returns {object} - returns an object domain.
*/
const options: RequestInit = {
method: 'GET',
headers: {
Expand Down Expand Up @@ -105,14 +139,14 @@ export default class Domains {
public async DomainPermissions (domainID: string, token: string): Promise<Permissions> {
// DomainPermissions retrieves domain permissions with provided ID.
/**
* @method DomainPermissions - retrieves domain permissions with provided ID.
* @param {string} domainID - domain ID.
* @param {string} token - user token.
* @returns {object} - returns an object domain permissions eg:
* { permissions: [ 'admin', 'edit', 'view', 'membership' ] }
* @example
* const domainID = "domainID";
*/
* @method DomainPermissions - retrieves domain permissions with provided ID.
* @param {string} domainID - domain ID.
* @param {string} token - user token.
* @returns {object} - returns an object domain permissions eg:
* { permissions: [ 'admin', 'edit', 'view', 'membership' ] }
* @example
* const domainID = "domainID";
*/
const options: RequestInit = {
method: 'GET',
headers: {
Expand All @@ -139,7 +173,12 @@ export default class Domains {

public async Domains (queryParams: QueryParams, token: string): Promise<DomainsPage> {
// Domains retrieves all domains.

/**
* @method Domains - retrieves all domains with provided query parameters.
* @param {object} queryParams - query parameters.
* @param {string} token - user token.
* @returns {object} - returns an object DomainsPage that shows a list of domains.
*/
const stringParams: Record<string, string> = Object.fromEntries(
Object.entries(queryParams).map(([key, value]) => [key, String(value)])
)
Expand All @@ -160,7 +199,7 @@ export default class Domains {
const errorRes = await response.json()
throw this.domainError.HandleError(errorRes.error, response.status)
}
const domainData = await response.json()
const domainData: DomainsPage = await response.json()
return domainData
} catch (error) {
throw error
Expand All @@ -169,7 +208,13 @@ export default class Domains {

public async ListUserDomains (userID: string, queryParams: QueryParams, token: string): Promise<DomainsPage> {
// ListUserDomains retrieves all domains for a user.

/**
* @method ListUserDomains - retrieves all domains for a user with provided query parameters.
* @param {string} userID - user ID.
* @param {object} queryParams - query parameters such as total, offset, limit.
* @param {string} token - user token.
* @returns {object} - returns an object DomainsPage that shows a list of domains.
*/
const stringParams: Record<string, string> = Object.fromEntries(
Object.entries(queryParams).map(([key, value]) => [key, String(value)])
)
Expand All @@ -190,15 +235,57 @@ export default class Domains {
const errorRes = await response.json()
throw this.domainError.HandleError(errorRes.error, response.status)
}
const domainData = await response.json()
const domainData: DomainsPage = await response.json()
return domainData
} catch (error) {
throw error
}
}

public async EnableDomain (domainID: string, token: string): Promise<Domain> {
public async ListDomainUsers (domainID: string, queryParams: QueryParams, token: string): Promise<UsersPage> {
// ListDomainUsers returns list of users for the given domain ID and filters.
/**
* @method ListDomainUsers - retrieves all users for a domain with provided query parameters.
* @param {string} domainID - domain ID.
* @param {object} queryParams - query parameters such as total, offset, limit.
* @param {string} token - user token.
* @returns {object} - returns an object UsersPage that shows a list of users.
*/
const stringParams: Record<string, string> = Object.fromEntries(
Object.entries(queryParams).map(([key, value]) => [key, String(value)])
)
const options: RequestInit = {
method: 'GET',
headers: {
'Content-Type': this.contentType,
Authorization: `Bearer ${token}`
}
}

try {
const response = await fetch(
new URL(`/domains/${domainID}/users?${new URLSearchParams(stringParams).toString()}`, this.usersUrl).toString(),
options
)
if (!response.ok) {
const errorRes = await response.json()
throw this.domainError.HandleError(errorRes.error, response.status)
}
const domainData: UsersPage = await response.json()
return domainData
} catch (error) {
throw error
}
}

public async EnableDomain (domainID: string, token: string): Promise<Response> {
// EnableDomain enables domain with provided ID.
/**
* @method EnableDomain - Enables domain with provided ID.
* @param {string} domainID - domain ID.
* @param {string} token - user token.
* @returns {object} - returns an object Response that carries the status code and a response message.
*/
const options: RequestInit = {
method: 'POST',
headers: {
Expand All @@ -216,15 +303,21 @@ export default class Domains {
const errorRes = await response.json()
throw this.domainError.HandleError(errorRes.error, response.status)
}
const domainData: Domain = await response.json()
return domainData
const enableResponse: Response = { status: response.status, message: 'Domain Enabled Successfully' }
return enableResponse
} catch (error) {
throw error
}
}

public async DisableDomain (domainID: string, token: string): Promise<Domain> {
public async DisableDomain (domainID: string, token: string): Promise<Response> {
// DisableDomain disables domain with provided ID.
/**
* @method DisableDomain - Disables domain with provided ID.
* @param {string} domainID - domain ID.
* @param {string} token - user token.
* @returns {object} - returns an object Response that carries the status code and a response message.
*/
const options: RequestInit = {
method: 'POST',
headers: {
Expand All @@ -242,15 +335,21 @@ export default class Domains {
const errorRes = await response.json()
throw this.domainError.HandleError(errorRes.error, response.status)
}
const domainData: Domain = await response.json()
return domainData
const disableResponse: Response = { status: response.status, message: 'Domain Disabled Successfully' }
return disableResponse
} catch (error) {
throw error
}
}

public async AddUsertoDomain (domainID: string, userIDs: string[], relation: string, token: string): Promise<Response> {
// AddUsertoDomain adds user to domain.
/**
* @method AddUsertoDomain - Adds user to domain.
* @param {string} domainID - domain ID.
* @param {array} userIDs - array of user IDs.
* @param {string} relation - user relation to domain such as 'administrator', 'member'.
*/
const req = { user_ids: userIDs, relation }
const options: RequestInit = {
method: 'POST',
Expand Down Expand Up @@ -279,6 +378,13 @@ export default class Domains {

public async RemoveUserfromDomain (domainID: string, userIDs: string[], relation: string, token: string): Promise<Response> {
// RemoveUserfromDomain removes user from domain.
/**
* @method RemoveUserfromDomain - Removes user from domain.
* @param {string} domainID - domain ID.
* @param {array} userIDs - array of user IDs.
* @param {string} relation - user relation to domain such as 'administrator', 'member'.
* @returns {object} - returns an object Response that carries the status code and a response message.
*/
const req = { user_ids: userIDs, relation }
const options: RequestInit = {
method: 'POST',
Expand Down
2 changes: 1 addition & 1 deletion src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class SDK {
hostUrl = defaultUrl
}: SDKConfig = {}) {
this.users = new Users({ usersUrl, thingsUrl, hostUrl })
this.domains = new Domains(domainsUrl)
this.domains = new Domains({ domainsUrl, usersUrl })
this.things = new Things(thingsUrl)
}
}
Expand Down

0 comments on commit 55fc80d

Please sign in to comment.