10 Commits

Author SHA1 Message Date
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
13e67d9012 1.0.4 2024-05-02 16:07:26 +02:00
62714c0166 fix(core): update 2024-05-02 16:07:25 +02:00
5f54bd07d7 1.0.3 2024-05-02 14:07:43 +02:00
602196197a fix(core): update 2024-05-02 14:07:42 +02:00
11 changed files with 289 additions and 50 deletions

View File

@ -5,10 +5,17 @@
"githost": "code.foss.global", "githost": "code.foss.global",
"gitscope": "apiclient.xyz", "gitscope": "apiclient.xyz",
"gitrepo": "zitadel", "gitrepo": "zitadel",
"description": "an unofficial zitadel client", "description": "An unofficial client for interacting with Zitadel API.",
"npmPackagename": "@apiclient.xyz/zitadel", "npmPackagename": "@apiclient.xyz/zitadel",
"license": "MIT", "license": "MIT",
"projectDomain": "apiclient.xyz" "projectDomain": "apiclient.xyz",
"keywords": [
"Zitadel",
"API client",
"TypeScript",
"authentication",
"user management"
]
} }
}, },
"npmci": { "npmci": {

View File

@ -1,8 +1,8 @@
{ {
"name": "@apiclient.xyz/zitadel", "name": "@apiclient.xyz/zitadel",
"version": "1.0.2", "version": "1.0.7",
"private": false, "private": false,
"description": "an unofficial zitadel client", "description": "An unofficial client for interacting with Zitadel API.",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts", "typings": "dist_ts/index.d.ts",
"type": "module", "type": "module",
@ -48,5 +48,12 @@
"cli.js", "cli.js",
"npmextra.json", "npmextra.json",
"readme.md" "readme.md"
],
"keywords": [
"Zitadel",
"API client",
"TypeScript",
"authentication",
"user management"
] ]
} }

0
readme.hints.md Normal file
View File

122
readme.md
View File

@ -1,31 +1,107 @@
# @apiclient.xyz/zitadel # @apiclient.xyz/zitadel
an unofficial zitadel client an unofficial zitadel client
## Availabililty and Links ## Install
* [npmjs.org (npm package)](https://www.npmjs.com/package/@apiclient.xyz/zitadel)
* [gitlab.com (source)](https://code.foss.global/apiclient.xyz/zitadel)
* [github.com (source mirror)](https://github.com/apiclient.xyz/zitadel)
* [docs (typedoc)](https://apiclient.xyz.gitlab.io/zitadel/)
## Status for master 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:
Status Category | Status Badge ```bash
-- | -- npm install @apiclient.xyz/zitadel --save
GitLab Pipelines | [![pipeline status](https://code.foss.global/apiclient.xyz/zitadel/badges/master/pipeline.svg)](https://lossless.cloud) ```
GitLab Pipline Test Coverage | [![coverage report](https://code.foss.global/apiclient.xyz/zitadel/badges/master/coverage.svg)](https://lossless.cloud)
npm | [![npm downloads per month](https://badgen.net/npm/dy/@apiclient.xyz/zitadel)](https://lossless.cloud) Ensure you have TypeScript installed for development purposes. If you haven't, install it using:
Snyk | [![Known Vulnerabilities](https://badgen.net/snyk/apiclient.xyz/zitadel)](https://lossless.cloud)
TypeScript Support | [![TypeScript](https://badgen.net/badge/TypeScript/>=%203.x/blue?icon=typescript)](https://lossless.cloud) ```bash
node Support | [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/) npm install -g typescript
Code Style | [![Code Style](https://badgen.net/badge/style/prettier/purple)](https://lossless.cloud) ```
PackagePhobia (total standalone install weight) | [![PackagePhobia](https://badgen.net/packagephobia/install/@apiclient.xyz/zitadel)](https://lossless.cloud)
PackagePhobia (package size on registry) | [![PackagePhobia](https://badgen.net/packagephobia/publish/@apiclient.xyz/zitadel)](https://lossless.cloud)
BundlePhobia (total size when bundled) | [![BundlePhobia](https://badgen.net/bundlephobia/minzip/@apiclient.xyz/zitadel)](https://lossless.cloud)
## Usage ## Usage
Use TypeScript for best in class intellisense
For further information read the linked docs at the top of this readme.
## Legal 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.
> MIT licensed | **©** [Task Venture Capital GmbH](https://task.vc)
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy) Let's get started by setting up and initializing the client in your project.
### Setup and Initialization
First, import the necessary classes from the package:
```typescript
import { ZitaldelClient, ZitaldelUser } from '@apiclient.xyz/zitadel';
```
Instantiate the `ZitaldelClient` by providing connection options, including the Zitadel instance URL and an 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
});
```
### Managing Users
The Zitadel client supports various user management operations such as listing users, getting user information, and creating new users.
#### Listing Users
To list users, you can use the `listUsers` method. This will return an array of `ZitaldelUser` objects:
```typescript
async function listUsers() {
try {
const users = await zitadelClient.listUsers();
console.log(users);
} catch (error) {
console.error('Failed to list users:', error);
}
}
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:
```typescript
async function getOwnUserInfo() {
try {
const user = await zitadelClient.listOwnUser();
console.log(user);
} catch (error) {
console.error('Failed to get user info:', error);
}
}
getOwnUserInfo();
```
#### 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:
```typescript
async function createUser() {
try {
await zitadelClient.createUser({
email: 'newuser@example.com',
firstName: 'John',
lastName: 'Doe'
});
console.log('User created successfully');
} catch (error) {
console.error('Failed to create user:', error);
}
}
createUser();
```
### Advanced Usage
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.
## Conclusion
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.
undefined

View File

@ -16,13 +16,23 @@ tap.test('first test', async () => {
}); });
tap.test('should list own user', async () => { tap.test('should list own user', async () => {
await testZitadel.listOwnUser(); const user = await testZitadel.listOwnUser();
console.log(user);
}) })
tap.test('should list users', async () => { tap.test('should list users', async () => {
const users = await testZitadel.listUsers(); const users = await testZitadel.listUsers();
expect(users).toBeArray(); expect(users).toBeArray();
expect(users[0]).toBeInstanceOf(zitadel.ZitaldelUser); expect(users[0]).toBeInstanceOf(zitadel.ZitaldelUser);
console.log(users);
}) })
tap.start() tap.test('should invite user', async () => {
await testZitadel.createUser({
email: await testQenv.getEnvVarOnDemand('ZITADEL_TEST_EMAIL'),
firstName: 'firstname',
lastName: 'lastname',
})
})
export default tap.start()

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@apiclient.xyz/zitadel', name: '@apiclient.xyz/zitadel',
version: '1.0.2', version: '1.0.7',
description: 'an unofficial zitadel client' description: 'An unofficial client for interacting with Zitadel API.'
} }

View File

@ -1,5 +1,6 @@
import { ZitadelProject } from './classes.zitadelproject.js';
import { ZitaldelUser } from './classes.zitadeluser.js'; import { ZitaldelUser } from './classes.zitadeluser.js';
import * as plugins from './zitadel.plugins.js'; import * as plugins from './plugins.js';
export interface IZitadelContructorOptions { export interface IZitadelContructorOptions {
url: string; url: string;
@ -10,6 +11,7 @@ export class ZitaldelClient {
private options: IZitadelContructorOptions; private options: IZitadelContructorOptions;
public authClient: plugins.zitadel.AuthServiceClient; public authClient: plugins.zitadel.AuthServiceClient;
public userClient: plugins.zitadel.UserServiceClient; public userClient: plugins.zitadel.UserServiceClient;
public managementClient: plugins.zitadel.ManagementServiceClient;
constructor(optionsArg: IZitadelContructorOptions) { constructor(optionsArg: IZitadelContructorOptions) {
this.options = optionsArg; this.options = optionsArg;
@ -22,26 +24,61 @@ export class ZitaldelClient {
this.options.url, this.options.url,
plugins.zitadel.createAccessTokenInterceptor(this.options.accessToken) plugins.zitadel.createAccessTokenInterceptor(this.options.accessToken)
); );
this.managementClient = plugins.zitadel.createManagementClient(
this.options.url,
plugins.zitadel.createAccessTokenInterceptor(this.options.accessToken)
);
} }
public async listOwnUser() { public async listOwnUser() {
const response = await this.authClient.getMyUser({}); const response = await this.authClient.getMyUser({});
console.log(response.user) 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() { public async listUsers() {
const response = await this.userClient.listUsers({}); const response = await this.userClient.listUsers({});
const returnArray: ZitaldelUser[] = []; const returnArray: ZitaldelUser[] = [];
for (const userObject of response.result) { for (const userObject of response.result) {
returnArray.push(await ZitaldelUser.fromApiUserObject(userObject)); returnArray.push(new ZitaldelUser(this, {
id: userObject.userId,
username: userObject.username,
lastLogin: null,
}));
} }
return returnArray; return returnArray;
} }
/** public async createUser(optionsArg: {
* invites a new user by email email: string;
*/ firstName: string;
public async inviteNewUserByEmail() { lastName: string;
}) {
const response = await this.userClient.addHumanUser({
email: {
email: optionsArg.email,
},
profile: {
givenName: optionsArg.firstName,
familyName: optionsArg.lastName,
}
});
console.log(response);
} }
} }

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,17 +1,67 @@
import * as plugins from './zitadel.plugins.js'; import type { ZitaldelClient } from './classes.zitadelclient.js';
import type { ZitadelProjectRole } from './classes.zitadelprojectrole.js';
import * as plugins from './plugins.js';
export type IZitadelApiUserObject = plugins.tsclass.typeFest.AsyncReturnType< export interface IZitadelUserData {
plugins.zitadel.UserServiceClient['listUsers'] id: string;
>['result'][0]; lastLogin: Date;
username: string;
}
export class ZitaldelUser { export class ZitaldelUser {
// STATIC // INSTANCE
static async fromApiUserObject(apiUserObject: IZitadelApiUserObject) { zitadelclientRef: ZitaldelClient;
const newUser = new ZitaldelUser(); data: IZitadelUserData;
newUser.data = apiUserObject;
return newUser; constructor(zitadelclientRefArg: ZitaldelClient, dataArg: IZitadelUserData) {
this.zitadelclientRef = zitadelclientRefArg;
this.data = dataArg;
} }
// INSTANCE public async addRole(projectRole: ZitadelProjectRole) {
data: IZitadelApiUserObject; 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,
});
}
} }