Compare commits

..

12 Commits

Author SHA1 Message Date
756e5eac36 1.1.0 2024-08-27 08:13:52 +02:00
6ab9ab2c59 feat(core): Add delete user functionality and necessary tests 2024-08-27 08:13:52 +02:00
66a4bf6142 1.0.9 2024-08-26 17:01:06 +02:00
d71197c6f7 fix(dependencies): Update dependencies to latest versions 2024-08-26 17:01:05 +02:00
fd07270149 1.0.8 2024-05-03 10:53:51 +02:00
d7444aa901 fix(core): update 2024-05-03 10:53:50 +02:00
26dd8ac6cd 1.0.7 2024-05-03 10:45:22 +02:00
78720a5e50 fix(core): update 2024-05-03 10:45:21 +02:00
19a4c9ef48 1.0.6 2024-05-03 10:39:25 +02:00
02a01ca35c fix(core): update 2024-05-03 10:39:24 +02:00
13fdbcebdc 1.0.5 2024-05-02 17:43:31 +02:00
6246a3d915 fix(core): update 2024-05-02 17:43:31 +02:00
12 changed files with 4054 additions and 2834 deletions

24
changelog.md Normal file
View File

@ -0,0 +1,24 @@
# Changelog
## 2024-08-27 - 1.1.0 - feat(core)
Add delete user functionality and necessary tests
- Implemented delete method in ZitadelUser class.
- Extended tests to cover user deletion functionality.
- Refactored createUser method to return a ZitadelUser instance.
## 2024-08-26 - 1.0.9 - fix(dependencies)
Update dependencies to latest versions
- Updated @git.zone/tsbuild from ^2.1.25 to ^2.1.84
- Updated @git.zone/tsrun from ^1.2.46 to ^1.2.49
- Updated @push.rocks/tapbundle from ^5.0.15 to ^5.0.24
- Updated @types/node from ^20.8.7 to ^22.5.0
- Updated @tsclass/tsclass from ^4.0.54 to ^4.1.2
- Updated @zitadel/node from ^2.0.8 to 2.0.17
## 2024-05-02 to 2024-05-03 - 1.0.1 to 1.0.8 - Fixes and Updates
General updates and fixes to the core functionality across multiple versions.
- Various fixes in core functionality
- Incremental improvements and updates

View File

@ -5,7 +5,7 @@
"githost": "code.foss.global",
"gitscope": "apiclient.xyz",
"gitrepo": "zitadel",
"description": "An unofficial client for interacting with Zitadel API.",
"description": "An unofficial client for interacting with Zitadel API, enabling user and project management functionalities.",
"npmPackagename": "@apiclient.xyz/zitadel",
"license": "MIT",
"projectDomain": "apiclient.xyz",
@ -14,7 +14,12 @@
"API client",
"TypeScript",
"authentication",
"user management"
"user management",
"project management",
"unofficial client",
"node.js",
"access control",
"software development"
]
}
},

View File

@ -1,8 +1,8 @@
{
"name": "@apiclient.xyz/zitadel",
"version": "1.0.4",
"version": "1.1.0",
"private": false,
"description": "An unofficial client for interacting with Zitadel API.",
"description": "An unofficial client for interacting with Zitadel API, enabling user and project management functionalities.",
"main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts",
"type": "module",
@ -14,17 +14,17 @@
"buildDocs": "(tsdoc)"
},
"devDependencies": {
"@git.zone/tsbuild": "^2.1.25",
"@git.zone/tsbuild": "^2.1.84",
"@git.zone/tsbundle": "^2.0.5",
"@git.zone/tsrun": "^1.2.46",
"@git.zone/tsrun": "^1.2.49",
"@git.zone/tstest": "^1.0.44",
"@push.rocks/qenv": "^6.0.5",
"@push.rocks/tapbundle": "^5.0.15",
"@types/node": "^20.8.7"
"@push.rocks/tapbundle": "^5.0.24",
"@types/node": "^22.5.0"
},
"dependencies": {
"@tsclass/tsclass": "^4.0.54",
"@zitadel/node": "^2.0.8"
"@tsclass/tsclass": "^4.1.2",
"@zitadel/node": "2.0.17"
},
"repository": {
"type": "git",
@ -54,6 +54,11 @@
"API client",
"TypeScript",
"authentication",
"user management"
"user management",
"project management",
"unofficial client",
"node.js",
"access control",
"software development"
]
}

6524
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

109
readme.md
View File

@ -1,15 +1,16 @@
# @apiclient.xyz/zitadel
an unofficial zitadel client
An unofficial client for interacting with Zitadel API.
## Install
To install `@apiclient.xyz/zitadel`, you need to have Node.js installed on your system. You can then add it to your project using npm with the following command:
To get started with `@apiclient.xyz/zitadel`, ensure you have Node.js installed on your machine. Then, you can add this package to your project by running:
```bash
npm install @apiclient.xyz/zitadel --save
```
Ensure you have TypeScript installed for development purposes. If you haven't, install it using:
This module is written in TypeScript to take advantage of type safety and autocompletion in IDEs. If you haven't installed TypeScript globally, you can do so by running:
```bash
npm install -g typescript
@ -17,91 +18,101 @@ npm install -g typescript
## Usage
The `@apiclient.xyz/zitadel` package provides a simplified, unofficial TypeScript client for interacting with Zitadel's APIs. This guide will walk you through setting up the client and executing various operations such as user management in a TypeScript project.
The `@apiclient.xyz/zitadel` package provides an unofficial TypeScript client for easy interaction with Zitadel's API, allowing for convenient management operations such as user and project management.
Let's get started by setting up and initializing the client in your project.
### Getting Started
### Setup and Initialization
First, import the necessary classes from the package:
First, ensure you import the core client class from the package:
```typescript
import { ZitaldelClient, ZitaldelUser } from '@apiclient.xyz/zitadel';
import { ZitaldelClient } from '@apiclient.xyz/zitadel';
```
Instantiate the `ZitaldelClient` by providing connection options, including the Zitadel instance URL and an access token:
Initialize the `ZitaldelClient` with your Zitadel instance URL and access token:
```typescript
const zitadelClient = new ZitaldelClient({
url: 'https://your-zitadel-instance.com', // Replace with your Zitadel instance URL
accessToken: 'your_access_token_here' // Replace with your actual access token
url: 'https://your-zitadel-instance.com', // Replace with your Zitadel instance URL.
accessToken: 'your_access_token_here' // Replace with your actual access token.
});
```
### Managing Users
### User Management
The Zitadel client supports various user management operations such as listing users, getting user information, and creating new users.
The library allows performing user operations such as listing all users, getting info of the current user, and creating new users.
#### Listing Users
To list users, you can use the `listUsers` method. This will return an array of `ZitaldelUser` objects:
#### List All Users
```typescript
async function listUsers() {
try {
const users = await zitadelClient.listUsers();
console.log(users);
} catch (error) {
console.error('Failed to list users:', error);
}
console.log(users); // Outputs user list
}
listUsers();
```
#### Getting User Information
To get information about a specific user, use the `listOwnUser` method. This method is handy for fetching details about the authenticated user:
#### Get Current User Information
```typescript
async function getOwnUserInfo() {
try {
async function getCurrentUser() {
const user = await zitadelClient.listOwnUser();
console.log(user);
} catch (error) {
console.error('Failed to get user info:', error);
}
console.log(user); // Outputs current user information.
}
getOwnUserInfo();
getCurrentUser();
```
#### Creating a User
To create a new user, the `createUser` method can be used. You need to provide user details such as email, first name, and last name:
#### Create a New User
```typescript
async function createUser() {
try {
await zitadelClient.createUser({
email: 'newuser@example.com',
firstName: 'John',
lastName: 'Doe'
firstName: 'First',
lastName: 'Last'
});
console.log('User created successfully');
} catch (error) {
console.error('Failed to create user:', error);
}
console.log('User created successfully.');
}
createUser();
```
### Advanced Usage
### Project Management
For more advanced scenarios, refer to the [official Zitadel documentation](https://docs.zitadel.ch/) and the source code of this package. The `@apiclient.xyz/zitadel` client provides a straightforward interface to Zitadel's API but does not cover all possible use cases and functionalities of Zitadel. You might need to extend the client or use the official Zitadel client for complex scenarios.
Beyond user management, the client also supports actions on projects—like listing and managing project roles.
## Conclusion
#### List Projects
The `@apiclient.xyz/zitadel` package makes it easier to interact with Zitadel's APIs from a TypeScript application. With it, you can manage users, handle authentication, and perform various other operations provided by Zitadel. This guide covered the basics of setting up the client, managing users, and performing some common operations. For further details or specific use cases, consult the Zitadel documentation and the package's source code.
```typescript
async function listProjects() {
const projects = await zitadelClient.listProjects();
console.log(projects); // Outputs list of projects
}
listProjects();
```
#### Project Roles
Each project comes with roles that can be managed through the client.
##### Listing Project Roles
To inspect roles within a project:
```typescript
async function listProjectRoles(projectId: string) {
const projectRoles = await someProject.listProjectRoles(); // Assume `someProject` is an instance of `ZitadelProject`.
console.log(projectRoles);
}
```
Note: The `ZitadelProject` object would typically be obtained as part of the list from `listProjects`.
### Advanced Topics
For more complex scenarios, such as direct interaction with Zitadel's gRPC APIs, or detailed configuration, please refer to the comprehensive [official Zitadel documentation](https://docs.zitadel.ch/). This client offers a simplified abstraction over Zitadel's API functionalities but can be extended or used in conjunction with direct API calls to meet specific needs.
For instance, the `ZitaldelClient` class can be tweaked to support custom interceptors for logging, authentication flow enhancements, or to integrate with Zitadel's event system for real-time updates on resources.
---
This is a basic introduction to `@apiclient.xyz/zitadel`. The client simplifies the interaction with Zitadel's API, focusing on core functionalities like user and project management. For specific use cases, detailed configurations, or advanced features, you may need to extend the client or use it as a foundation for more complex interactions with the Zitadel API.
undefined

View File

@ -24,15 +24,24 @@ tap.test('should list users', async () => {
const users = await testZitadel.listUsers();
expect(users).toBeArray();
expect(users[0]).toBeInstanceOf(zitadel.ZitaldelUser);
console.log(users);
console.log(JSON.stringify(users, null, 2));
})
tap.test('should invite user', async () => {
await testZitadel.createUser({
const createUserTest = tap.test('should invite user', async (toolsArg) => {
const user = await testZitadel.createUser({
email: await testQenv.getEnvVarOnDemand('ZITADEL_TEST_EMAIL'),
firstName: 'firstname',
lastName: 'lastname',
})
await toolsArg.delayFor(5000);
return user;
})
tap.test('should delete user', async () => {
const createdUser: zitadel.ZitaldelUser = (await createUserTest.testResultPromise) as any;
expect(createdUser).toBeInstanceOf(zitadel.ZitaldelUser);
await createdUser.delete();
})
export default tap.start()

View File

@ -1,8 +1,8 @@
/**
* autocreated commitinfo by @pushrocks/commitinfo
* autocreated commitinfo by @push.rocks/commitinfo
*/
export const commitinfo = {
name: '@apiclient.xyz/zitadel',
version: '1.0.4',
description: 'An unofficial client for interacting with Zitadel API.'
version: '1.1.0',
description: 'An unofficial client for interacting with Zitadel API, enabling user and project management functionalities.'
}

View File

@ -1,5 +1,6 @@
import { ZitadelProject } from './classes.zitadelproject.js';
import { ZitaldelUser } from './classes.zitadeluser.js';
import * as plugins from './zitadel.plugins.js';
import * as plugins from './plugins.js';
export interface IZitadelContructorOptions {
url: string;
@ -10,6 +11,7 @@ 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;
@ -22,6 +24,10 @@ export class ZitaldelClient {
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() {
@ -34,6 +40,18 @@ export class ZitaldelClient {
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[] = [];
@ -61,6 +79,12 @@ export class ZitaldelClient {
familyName: optionsArg.lastName,
}
});
console.log(response);
// const allUsers = await this.listUsers();
// console.log(JSON.stringify(allUsers, null, 2));
return new ZitaldelUser(this, {
id: response.userId,
lastLogin: null,
username: optionsArg.email,
});
}
}

View File

@ -0,0 +1,33 @@
import type { ZitaldelClient } from './classes.zitadelclient.js';
import { ZitadelProjectRole } from './classes.zitadelprojectrole.js';
import * as plugins from './plugins.js';
export class IZitadelProjectData {
id: string;
name: string;
}
export class ZitadelProject {
ziadelclientRef: ZitaldelClient;
public data: IZitadelProjectData;
constructor(zitadelclientRefArg: ZitaldelClient, dataArg: IZitadelProjectData) {
this.ziadelclientRef = zitadelclientRefArg;
this.data = dataArg;
}
public async listProjectRoles() {
const returnRoles: ZitadelProjectRole[] = [];
const response = await this.ziadelclientRef.managementClient.listProjectRoles({
projectId: this.data.id,
});
for (const roleObject of response.result) {
returnRoles.push(new ZitadelProjectRole(this.ziadelclientRef, {
project: this,
name: roleObject.displayName,
key: roleObject.key,
}));
}
return returnRoles;
}
}

View File

@ -0,0 +1,19 @@
import type { ZitaldelClient } from './classes.zitadelclient.js';
import type { ZitadelProject } from './classes.zitadelproject.js';
import * as plugins from './plugins.js';
export interface IZitadelProjectRoleData {
project: ZitadelProject;
key: string;
name: string;
}
export class ZitadelProjectRole {
ziadelclientRef: ZitaldelClient;
public data: IZitadelProjectRoleData;
constructor(zitadelclientRefArg: ZitaldelClient, dataArg: IZitadelProjectRoleData) {
this.ziadelclientRef = zitadelclientRefArg;
this.data = dataArg;
}
}

View File

@ -1,20 +1,76 @@
import type { ZitaldelClient } from './classes.zitadelclient.js';
import * as plugins from './zitadel.plugins.js';
import type { ZitadelProjectRole } from './classes.zitadelprojectrole.js';
import * as plugins from './plugins.js';
export interface IZitadelUserData {
id: string;
lastLogin: Date;
username: string;
};
}
export class ZitaldelUser {
// INSTANCE
zitadelclientRef: ZitaldelClient;
data: IZitadelUserData;
constructor(zitadelclientRefArg: ZitaldelClient, dataArg: IZitadelUserData) {
this.zitadelclientRef = zitadelclientRefArg;
this.data = dataArg;
}
// INSTANCE
data: IZitadelUserData;
public async addRole(projectRole: ZitadelProjectRole) {
const response = await this.zitadelclientRef.managementClient.addUserGrant({
userId: this.data.id,
roleKeys: [projectRole.data.key],
projectId: projectRole.data.project.data.id,
});
}
/**
* change email address of user,
* optionally supply own url for email verification
* @param emailAddress
* @param verificationUrl
*/
public async changeEmail(optionsArg: { emailAddress: string; verificationUrl?: string }) {
const response = await this.zitadelclientRef.userClient.setEmail({
userId: this.data.id,
email: optionsArg.emailAddress,
...(optionsArg.verificationUrl
? {
sendCode: {
urlTemplate: optionsArg.verificationUrl,
},
}
: {}),
});
}
public async setPassword(optionsArg: { password: string; changeRequired?: boolean }) {
const response = await this.zitadelclientRef.userClient.setPassword({
userId: this.data.id,
newPassword: {
password: optionsArg.password,
...(optionsArg.changeRequired ? { changeRequired: optionsArg.changeRequired } : {}),
},
});
}
/**
* triggers a password reset action for the user
*/
public async resetPassword() {
const response = await this.zitadelclientRef.userClient.passwordReset({
userId: this.data.id,
});
}
/**
* deletes the user
*/
public async delete() {
const response = await this.zitadelclientRef.userClient.deleteUser({
userId: this.data.id,
});
}
}