Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
13fdbcebdc | |||
6246a3d915 | |||
13e67d9012 | |||
62714c0166 | |||
5f54bd07d7 | |||
602196197a |
@ -5,10 +5,17 @@
|
||||
"githost": "code.foss.global",
|
||||
"gitscope": "apiclient.xyz",
|
||||
"gitrepo": "zitadel",
|
||||
"description": "an unofficial zitadel client",
|
||||
"description": "An unofficial client for interacting with Zitadel API.",
|
||||
"npmPackagename": "@apiclient.xyz/zitadel",
|
||||
"license": "MIT",
|
||||
"projectDomain": "apiclient.xyz"
|
||||
"projectDomain": "apiclient.xyz",
|
||||
"keywords": [
|
||||
"Zitadel",
|
||||
"API client",
|
||||
"TypeScript",
|
||||
"authentication",
|
||||
"user management"
|
||||
]
|
||||
}
|
||||
},
|
||||
"npmci": {
|
||||
|
11
package.json
11
package.json
@ -1,8 +1,8 @@
|
||||
{
|
||||
"name": "@apiclient.xyz/zitadel",
|
||||
"version": "1.0.2",
|
||||
"version": "1.0.5",
|
||||
"private": false,
|
||||
"description": "an unofficial zitadel client",
|
||||
"description": "An unofficial client for interacting with Zitadel API.",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
"type": "module",
|
||||
@ -48,5 +48,12 @@
|
||||
"cli.js",
|
||||
"npmextra.json",
|
||||
"readme.md"
|
||||
],
|
||||
"keywords": [
|
||||
"Zitadel",
|
||||
"API client",
|
||||
"TypeScript",
|
||||
"authentication",
|
||||
"user management"
|
||||
]
|
||||
}
|
||||
|
0
readme.hints.md
Normal file
0
readme.hints.md
Normal file
122
readme.md
122
readme.md
@ -1,31 +1,107 @@
|
||||
# @apiclient.xyz/zitadel
|
||||
an unofficial zitadel client
|
||||
|
||||
## Availabililty and Links
|
||||
* [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/)
|
||||
## Install
|
||||
|
||||
## 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
|
||||
-- | --
|
||||
GitLab Pipelines | [](https://lossless.cloud)
|
||||
GitLab Pipline Test Coverage | [](https://lossless.cloud)
|
||||
npm | [](https://lossless.cloud)
|
||||
Snyk | [](https://lossless.cloud)
|
||||
TypeScript Support | [](https://lossless.cloud)
|
||||
node Support | [](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
Code Style | [](https://lossless.cloud)
|
||||
PackagePhobia (total standalone install weight) | [](https://lossless.cloud)
|
||||
PackagePhobia (package size on registry) | [](https://lossless.cloud)
|
||||
BundlePhobia (total size when bundled) | [](https://lossless.cloud)
|
||||
```bash
|
||||
npm install @apiclient.xyz/zitadel --save
|
||||
```
|
||||
|
||||
Ensure you have TypeScript installed for development purposes. If you haven't, install it using:
|
||||
|
||||
```bash
|
||||
npm install -g typescript
|
||||
```
|
||||
|
||||
## Usage
|
||||
Use TypeScript for best in class intellisense
|
||||
For further information read the linked docs at the top of this readme.
|
||||
|
||||
## Legal
|
||||
> MIT licensed | **©** [Task Venture Capital GmbH](https://task.vc)
|
||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
||||
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.
|
||||
|
||||
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
|
14
test/test.ts
14
test/test.ts
@ -16,13 +16,23 @@ tap.test('first test', 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 () => {
|
||||
const users = await testZitadel.listUsers();
|
||||
expect(users).toBeArray();
|
||||
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()
|
||||
|
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@apiclient.xyz/zitadel',
|
||||
version: '1.0.2',
|
||||
description: 'an unofficial zitadel client'
|
||||
version: '1.0.5',
|
||||
description: 'An unofficial client for interacting with Zitadel API.'
|
||||
}
|
||||
|
@ -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,26 +24,61 @@ 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() {
|
||||
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() {
|
||||
const response = await this.userClient.listUsers({});
|
||||
const returnArray: ZitaldelUser[] = [];
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* invites a new user by email
|
||||
*/
|
||||
public async inviteNewUserByEmail() {
|
||||
|
||||
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,
|
||||
}
|
||||
});
|
||||
console.log(response);
|
||||
}
|
||||
}
|
||||
|
33
ts/classes.zitadelproject.ts
Normal file
33
ts/classes.zitadelproject.ts
Normal 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;
|
||||
}
|
||||
}
|
19
ts/classes.zitadelprojectrole.ts
Normal file
19
ts/classes.zitadelprojectrole.ts
Normal 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;
|
||||
}
|
||||
}
|
@ -1,17 +1,29 @@
|
||||
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<
|
||||
plugins.zitadel.UserServiceClient['listUsers']
|
||||
>['result'][0];
|
||||
export interface IZitadelUserData {
|
||||
id: string;
|
||||
lastLogin: Date;
|
||||
username: string;
|
||||
};
|
||||
|
||||
export class ZitaldelUser {
|
||||
// STATIC
|
||||
static async fromApiUserObject(apiUserObject: IZitadelApiUserObject) {
|
||||
const newUser = new ZitaldelUser();
|
||||
newUser.data = apiUserObject;
|
||||
return newUser;
|
||||
// INSTANCE
|
||||
zitadelclientRef: ZitaldelClient;
|
||||
data: IZitadelUserData;
|
||||
|
||||
constructor(zitadelclientRefArg: ZitaldelClient, dataArg: IZitadelUserData) {
|
||||
this.zitadelclientRef = zitadelclientRefArg;
|
||||
this.data = dataArg;
|
||||
}
|
||||
|
||||
public async addRole(projectRole: ZitadelProjectRole) {
|
||||
this.zitadelclientRef.managementClient.addUserGrant({
|
||||
userId: this.data.id,
|
||||
roleKeys: [projectRole.data.key],
|
||||
projectId: projectRole.data.project.data.id,
|
||||
});
|
||||
}
|
||||
|
||||
// INSTANCE
|
||||
data: IZitadelApiUserObject;
|
||||
}
|
||||
|
Reference in New Issue
Block a user