From 314379da0f53c44c7b504aea6ca232c44e84ffc8 Mon Sep 17 00:00:00 2001 From: Thomas Faust Date: Sat, 15 Mar 2025 18:18:05 +0100 Subject: [PATCH] feat(zitadel-proto): add CJS and ESM support; export in Zitadel api structure; add build configuration --- packages/zitadel-proto/README.md | 83 +++++++++++++++++++++--- packages/zitadel-proto/index.ts | 5 ++ packages/zitadel-proto/package.json | 28 ++++++-- packages/zitadel-proto/test/cjs-test.cjs | 28 ++++++++ packages/zitadel-proto/test/esm-test.mjs | 28 ++++++++ packages/zitadel-proto/tsup.config.ts | 13 ++++ packages/zitadel-proto/v1.ts | 53 +++++++++++++++ packages/zitadel-proto/v2.ts | 49 ++++++++++++++ packages/zitadel-proto/v3alpha.ts | 9 +++ 9 files changed, 282 insertions(+), 14 deletions(-) create mode 100644 packages/zitadel-proto/index.ts create mode 100644 packages/zitadel-proto/test/cjs-test.cjs create mode 100644 packages/zitadel-proto/test/esm-test.mjs create mode 100644 packages/zitadel-proto/tsup.config.ts create mode 100644 packages/zitadel-proto/v1.ts create mode 100644 packages/zitadel-proto/v2.ts create mode 100644 packages/zitadel-proto/v3alpha.ts diff --git a/packages/zitadel-proto/README.md b/packages/zitadel-proto/README.md index bf8a064c12..32bf3236c7 100644 --- a/packages/zitadel-proto/README.md +++ b/packages/zitadel-proto/README.md @@ -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 diff --git a/packages/zitadel-proto/index.ts b/packages/zitadel-proto/index.ts new file mode 100644 index 0000000000..6b7b927f11 --- /dev/null +++ b/packages/zitadel-proto/index.ts @@ -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 }; diff --git a/packages/zitadel-proto/package.json b/packages/zitadel-proto/package.json index dcfa9b3f22..5a1b2fee1d 100644 --- a/packages/zitadel-proto/package.json +++ b/packages/zitadel-proto/package.json @@ -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" } -} +} \ No newline at end of file diff --git a/packages/zitadel-proto/test/cjs-test.cjs b/packages/zitadel-proto/test/cjs-test.cjs new file mode 100644 index 0000000000..87033cfd3b --- /dev/null +++ b/packages/zitadel-proto/test/cjs-test.cjs @@ -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); +} diff --git a/packages/zitadel-proto/test/esm-test.mjs b/packages/zitadel-proto/test/esm-test.mjs new file mode 100644 index 0000000000..aafaf01df3 --- /dev/null +++ b/packages/zitadel-proto/test/esm-test.mjs @@ -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); +} diff --git a/packages/zitadel-proto/tsup.config.ts b/packages/zitadel-proto/tsup.config.ts new file mode 100644 index 0000000000..1fa9c664a7 --- /dev/null +++ b/packages/zitadel-proto/tsup.config.ts @@ -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", +}); diff --git a/packages/zitadel-proto/v1.ts b/packages/zitadel-proto/v1.ts new file mode 100644 index 0000000000..bea11ca384 --- /dev/null +++ b/packages/zitadel-proto/v1.ts @@ -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, +}; diff --git a/packages/zitadel-proto/v2.ts b/packages/zitadel-proto/v2.ts new file mode 100644 index 0000000000..532db0d82e --- /dev/null +++ b/packages/zitadel-proto/v2.ts @@ -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, +}; diff --git a/packages/zitadel-proto/v3alpha.ts b/packages/zitadel-proto/v3alpha.ts new file mode 100644 index 0000000000..e6b2c33464 --- /dev/null +++ b/packages/zitadel-proto/v3alpha.ts @@ -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 };