feat(zitadel-proto): add CJS and ESM support; export in Zitadel api structure; add build configuration

This commit is contained in:
Thomas Faust
2025-03-15 18:18:05 +01:00
parent f310277add
commit 314379da0f
9 changed files with 282 additions and 14 deletions

View File

@@ -8,22 +8,87 @@ To install the package, use npm or yarn:
```sh
npm install @zitadel/proto
```
or
```sh
# or
yarn add @zitadel/proto
# or
pnpm add @zitadel/proto
```
## Usage
To use the proto definitions in your project, import the generated code:
This package supports both ESM and CommonJS imports. The API is organized into version-specific namespaces: `v1`, `v2`, and `v3alpha`.
```ts
import { Organization } from "@zitadel/proto/zitadel/org/v2/org_pb";
### ESM (ECMAScript Modules)
const org: Organization | null = await getDefaultOrg();
```typescript
// Import the entire package
import * as zitadel from "@zitadel/proto";
// Use the version-specific namespaces
const userRequest = new zitadel.v1.user.GetUserRequest();
// Or import specific versions
import { v2 } from "@zitadel/proto";
const userServiceRequest = new v2.user_service.GetUserRequest();
```
### CommonJS
```typescript
// Import the entire package
const zitadel = require("@zitadel/proto");
// Use the version-specific namespaces
const userRequest = new zitadel.v1.user.GetUserRequest();
```
## API Structure
The package is organized into version-specific namespaces:
- `v1`: Contains the original ZITADEL API
- `v2`: Contains the newer version of the API with improved organization
- `v3alpha`: Contains the alpha version of the upcoming API
## Package Structure
The package is organized as follows:
- `index.ts`: Main entry point that exports the version-specific APIs
- `v1.ts`: Exports all v1 API modules
- `v2.ts`: Exports all v2 API modules
- `v3alpha.ts`: Exports all v3alpha API modules
- `zitadel/`: Contains the generated proto files
## Development
### Generating the proto files
The proto files are generated from the ZITADEL API definitions using [buf](https://buf.build/).
```sh
pnpm generate
```
### Building the package
```sh
pnpm build
```
### Testing
To test both ESM and CommonJS imports:
```sh
pnpm test
```
Or test them individually:
```bash
pnpm test:cjs # Test CommonJS imports
pnpm test:esm # Test ESM imports
```
## Documentation

View File

@@ -0,0 +1,5 @@
import * as v1 from "./v1.js";
import * as v2 from "./v2.js";
import * as v3alpha from "./v3alpha.js";
export { v1, v2, v3alpha };

View File

@@ -5,22 +5,40 @@
"publishConfig": {
"access": "public"
},
"type": "module",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.js"
}
},
"files": [
"index.ts",
"v1.ts",
"v2.ts",
"v3alpha.ts",
"zitadel/**",
"validate/**",
"google/**",
"protoc-gen-openapiv2/**"
"dist/**"
],
"sideEffects": false,
"scripts": {
"generate": "buf generate https://github.com/zitadel/zitadel.git#tag=v2.71.1 --path ./proto/zitadel",
"clean": "rm -rf zitadel .turbo node_modules google protoc-gen-openapiv2 validate"
"clean": "rm -rf zitadel .turbo node_modules google protoc-gen-openapiv2 validate",
"build": "tsup",
"test:cjs": "node test/cjs-test.cjs",
"test:esm": "node test/esm-test.mjs",
"test": "pnpm build && pnpm test:cjs && pnpm test:esm"
},
"dependencies": {
"@bufbuild/protobuf": "^2.2.2"
},
"devDependencies": {
"@bufbuild/buf": "^1.47.2"
"@bufbuild/buf": "^1.47.2",
"tsup": "^8.0.0"
}
}
}

View File

@@ -0,0 +1,28 @@
// CommonJS import test
const zitadel = require("@zitadel/proto");
// Check if the import worked by accessing some properties
console.log("CommonJS import test:");
console.log("- Has v1 API:", !!zitadel.v1);
console.log("- Has v2 API:", !!zitadel.v2);
console.log("- Has v3alpha API:", !!zitadel.v3alpha);
// Test v1 API
console.log("- v1.user module:", !!zitadel.v1.user);
console.log("- v1.management module:", !!zitadel.v1.management);
// Test v2 API
console.log("- v2.user module:", !!zitadel.v2.user);
console.log("- v2.user_service module:", !!zitadel.v2.user_service);
// Test v3alpha API
console.log("- v3alpha.user module:", !!zitadel.v3alpha.user);
console.log("- v3alpha.user_service module:", !!zitadel.v3alpha.user_service);
// Test successful if we can access these modules
if (zitadel.v1 && zitadel.v2 && zitadel.v3alpha) {
console.log("✅ CommonJS import test passed!");
} else {
console.error("❌ CommonJS import test failed!");
process.exit(1);
}

View File

@@ -0,0 +1,28 @@
// ESM import test
import * as zitadel from "@zitadel/proto";
// Check if the import worked by accessing some properties
console.log("ESM import test:");
console.log("- Has v1 API:", !!zitadel.v1);
console.log("- Has v2 API:", !!zitadel.v2);
console.log("- Has v3alpha API:", !!zitadel.v3alpha);
// Test v1 API
console.log("- v1.user module:", !!zitadel.v1.user);
console.log("- v1.management module:", !!zitadel.v1.management);
// Test v2 API
console.log("- v2.user module:", !!zitadel.v2.user);
console.log("- v2.user_service module:", !!zitadel.v2.user_service);
// Test v3alpha API
console.log("- v3alpha.user module:", !!zitadel.v3alpha.user);
console.log("- v3alpha.user_service module:", !!zitadel.v3alpha.user_service);
// Test successful if we can access these modules
if (zitadel.v1 && zitadel.v2 && zitadel.v3alpha) {
console.log("✅ ESM import test passed!");
} else {
console.error("❌ ESM import test failed!");
process.exit(1);
}

View File

@@ -0,0 +1,13 @@
import { defineConfig } from "tsup";
export default defineConfig({
entry: ["index.ts"],
dts: true,
clean: true,
minify: false,
splitting: false,
sourcemap: true,
format: ["esm", "cjs"],
platform: "neutral",
target: "node16",
});

View File

@@ -0,0 +1,53 @@
import * as action from "./zitadel/action_pb.js";
import * as admin from "./zitadel/admin_pb.js";
import * as app from "./zitadel/app_pb.js";
import * as auth_n_key from "./zitadel/auth_n_key_pb.js";
import * as auth from "./zitadel/auth_pb.js";
import * as change from "./zitadel/change_pb.js";
import * as event from "./zitadel/event_pb.js";
import * as feature from "./zitadel/feature_pb.js";
import * as idp from "./zitadel/idp_pb.js";
import * as instance from "./zitadel/instance_pb.js";
import * as management from "./zitadel/management_pb.js";
import * as member from "./zitadel/member_pb.js";
import * as message from "./zitadel/message_pb.js";
import * as metadata from "./zitadel/metadata_pb.js";
import * as object from "./zitadel/object_pb.js";
import * as options from "./zitadel/options_pb.js";
import * as org from "./zitadel/org_pb.js";
import * as policy from "./zitadel/policy_pb.js";
import * as project from "./zitadel/project_pb.js";
import * as quota from "./zitadel/quota_pb.js";
import * as settings from "./zitadel/settings_pb.js";
import * as system from "./zitadel/system_pb.js";
import * as text from "./zitadel/text_pb.js";
import * as user from "./zitadel/user_pb.js";
import * as v1 from "./zitadel/v1_pb.js";
export {
action,
admin,
app,
auth,
auth_n_key,
change,
event,
feature,
idp,
instance,
management,
member,
message,
metadata,
object,
options,
org,
policy,
project,
quota,
settings,
system,
text,
user,
v1,
};

View File

@@ -0,0 +1,49 @@
import * as feature from "./zitadel/feature/v2/feature_pb.js";
import * as feature_service from "./zitadel/feature/v2/feature_service_pb.js";
import * as idp from "./zitadel/idp/v2/idp_pb.js";
import * as idp_service from "./zitadel/idp/v2/idp_service_pb.js";
import * as object from "./zitadel/object/v2/object_pb.js";
import * as oidc_authorization from "./zitadel/oidc/v2/authorization_pb.js";
import * as oidc_service from "./zitadel/oidc/v2/oidc_service_pb.js";
import * as org from "./zitadel/org/v2/org_pb.js";
import * as org_service from "./zitadel/org/v2/org_service_pb.js";
import * as saml_authorization from "./zitadel/saml/v2/authorization_pb.js";
import * as saml_service from "./zitadel/saml/v2/saml_service_pb.js";
import * as session from "./zitadel/session/v2/session_pb.js";
import * as session_service from "./zitadel/session/v2/session_service_pb.js";
import * as settings from "./zitadel/settings/v2/settings_pb.js";
import * as settings_service from "./zitadel/settings/v2/settings_service_pb.js";
import * as user_auth from "./zitadel/user/v2/auth_pb.js";
import * as user_email from "./zitadel/user/v2/email_pb.js";
import * as user_idp from "./zitadel/user/v2/idp_pb.js";
import * as user_password from "./zitadel/user/v2/password_pb.js";
import * as user_phone from "./zitadel/user/v2/phone_pb.js";
import * as user_query from "./zitadel/user/v2/query_pb.js";
import * as user from "./zitadel/user/v2/user_pb.js";
import * as user_service from "./zitadel/user/v2/user_service_pb.js";
export {
feature,
feature_service,
idp,
idp_service,
object,
oidc_authorization,
oidc_service,
org,
org_service,
saml_authorization,
saml_service,
session,
session_service,
settings,
settings_service,
user,
user_auth,
user_email,
user_idp,
user_password,
user_phone,
user_query,
user_service,
};

View File

@@ -0,0 +1,9 @@
import * as user_authenticator from "./zitadel/resources/user/v3alpha/authenticator_pb.js";
import * as user_communication from "./zitadel/resources/user/v3alpha/communication_pb.js";
import * as user_query from "./zitadel/resources/user/v3alpha/query_pb.js";
import * as user from "./zitadel/resources/user/v3alpha/user_pb.js";
import * as user_service from "./zitadel/resources/user/v3alpha/user_service_pb.js";
import * as user_schema from "./zitadel/resources/userschema/v3alpha/user_schema_pb.js";
import * as user_schema_service from "./zitadel/resources/userschema/v3alpha/user_schema_service_pb.js";
export { user, user_authenticator, user_communication, user_query, user_schema, user_schema_service, user_service };