zitadel/ts/classes.zitadelclient.ts

91 lines
2.7 KiB
TypeScript

import { ZitadelProject } from './classes.zitadelproject.js';
import { ZitaldelUser } from './classes.zitadeluser.js';
import * as plugins from './plugins.js';
export interface IZitadelContructorOptions {
url: string;
accessToken: string;
}
export class ZitaldelClient {
private options: IZitadelContructorOptions;
public authClient: plugins.zitadel.AuthServiceClient;
public userClient: plugins.zitadel.UserServiceClient;
public managementClient: plugins.zitadel.ManagementServiceClient;
constructor(optionsArg: IZitadelContructorOptions) {
this.options = optionsArg;
console.log(this.options);
this.authClient = plugins.zitadel.createAuthClient(
this.options.url,
plugins.zitadel.createAccessTokenInterceptor(this.options.accessToken)
);
this.userClient = plugins.zitadel.createUserClient(
this.options.url,
plugins.zitadel.createAccessTokenInterceptor(this.options.accessToken)
);
this.managementClient = plugins.zitadel.createManagementClient(
this.options.url,
plugins.zitadel.createAccessTokenInterceptor(this.options.accessToken)
);
}
public async listOwnUser() {
const response = await this.authClient.getMyUser({});
const zitadelUser = new ZitaldelUser(this, {
id: response.user.id,
lastLogin: response.lastLogin,
username: response.user.userName,
});
return zitadelUser;
}
public async listProjects() {
const returnProjects: ZitadelProject[] = [];
const response = await this.managementClient.listProjects({});
for (const projectObject of response.result) {
returnProjects.push(new ZitadelProject(this, {
id: projectObject.id,
name: projectObject.name,
}));
}
return returnProjects;
}
public async listUsers() {
const response = await this.userClient.listUsers({});
const returnArray: ZitaldelUser[] = [];
for (const userObject of response.result) {
returnArray.push(new ZitaldelUser(this, {
id: userObject.userId,
username: userObject.username,
lastLogin: null,
}));
}
return returnArray;
}
public async createUser(optionsArg: {
email: string;
firstName: string;
lastName: string;
}) {
const response = await this.userClient.addHumanUser({
email: {
email: optionsArg.email,
},
profile: {
givenName: optionsArg.firstName,
familyName: optionsArg.lastName,
}
});
// const allUsers = await this.listUsers();
// console.log(JSON.stringify(allUsers, null, 2));
return new ZitaldelUser(this, {
id: response.userId,
lastLogin: null,
username: optionsArg.email,
});
}
}