move server to root, path, example

This commit is contained in:
Max Peintner
2023-04-20 14:26:55 +02:00
parent 26cf9132a0
commit d154f07f1c
12 changed files with 125 additions and 92 deletions

View File

@@ -1,20 +1,16 @@
import { ZitadelOptions } from "@zitadel/server";
import { ZitadelServerOptions, getServers } from "@zitadel/server";
import { getAuth } from "@zitadel/server/auth";
import { getApp, getApps, initializeApp } from "@zitadel/server/app";
export const zitadelConfig: ZitadelOptions = {
export const zitadelConfig: ZitadelServerOptions = {
apiUrl: process.env.ZITADEL_API_URL ?? "",
projectId: process.env.ZITADEL_PROJECT_ID ?? "",
appId: process.env.ZITADEL_APP_ID ?? "",
token: "this should be a pat",
token: process.env.ZITADEL_SERVICE_USER_TOKEN ?? "",
};
if (!getApps().length) {
initializeApp(zitadelConfig);
if (!getServers().length) {
initializeServer(zitadelConfig);
}
const app = getApp();
const server = getServer();
export async function getMyUser(): Promise<GetMyUserResponse> {
const auth = await getAuth();

View File

@@ -0,0 +1,28 @@
import {
ZitadelServerOptions,
getServer,
getServers,
initializeServer,
} from "#/server";
import { GetMyUserResponse, getAuth } from "#/auth";
async function getMyUser(): Promise<GetMyUserResponse> {
const auth = await getAuth();
const response = await auth.getMyUser({});
return response;
}
async function main() {
const zitadelConfig: ZitadelServerOptions = {
apiUrl: "https://dev-mfhquc.zitadel.cloud/",
token: "123",
};
if (!getServers().length) {
initializeServer(zitadelConfig);
}
const app = getServer();
}
main();

View File

@@ -9,9 +9,13 @@
"files": [
"dist/**"
],
"exports": {
".": "./index.js",
"./auth": "./jwt/auth.js"
},
"scripts": {
"build": "tsup src/index.ts src/auth/index.ts src/app/index.ts --format esm,cjs --dts",
"dev": "tsup src/index.ts src/auth/index.ts src/app/index.ts --format esm,cjs --watch --dts",
"build": "tsup src/index.ts src/*/index.ts --format esm,cjs --dts",
"dev": "tsup src/index.ts src/*/index.ts --format esm,cjs --watch --dts",
"lint": "eslint \"src/**/*.ts*\"",
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
"prebuild": "pnpm run generate",

View File

@@ -1,39 +0,0 @@
let apps: ZitadelApp[] = [];
export interface ZitadelClientProps {
appId: string;
projectId: string;
apiUrl: string; // process.env.ZITADEL_API_URL
token: string;
adminToken?: string;
managementToken?: string;
}
export interface ZitadelOptions extends ZitadelClientProps {
name?: string;
}
export interface ZitadelApp {
name: string | undefined;
config: ZitadelClientProps;
}
export async function initializeApp(
config: ZitadelClientProps,
name?: string
): Promise<ZitadelApp> {
const app = { config, name };
return app;
}
export function getApps(): ZitadelApp[] {
return apps;
}
export function getApp(name?: string): ZitadelApp | undefined {
return name
? apps.find((a) => a.name === name)
: apps.length === 1
? apps[0]
: undefined;
}

View File

@@ -1 +0,0 @@
export * from "./app";

View File

@@ -5,7 +5,7 @@ import {
AuthServiceDefinition,
GetMyUserResponse,
} from "../proto/server/zitadel/auth";
import { ZitadelApp } from "../app/app";
import { ZitadelServer } from "../server";
import { authMiddleware } from "../middleware";
const createClient = <Client>(
@@ -18,7 +18,7 @@ const createClient = <Client>(
.create(definition, channel) as Client;
};
export async function getAuth(app?: ZitadelApp): Promise<AuthServiceClient> {
export async function getAuth(app?: ZitadelServer): Promise<AuthServiceClient> {
return createClient<AuthServiceClient>(
AuthServiceDefinition as CompatServiceDefinition,
""

View File

@@ -1,9 +1,10 @@
export * from "./app/app";
export * from "./server";
export * from "./middleware";
export * as auth from "./auth";
export * as management from "./management";
export * as admin from "./admin";
export * as system from "./system";
// export * as auth from "./auth";
// export * as management from "./management";
// export * as admin from "./admin";
// export * as system from "./system";
// export * as proto from "./proto/server/zitadel/*";
// export * from "./proto/server/zitadel/management";

View File

@@ -7,7 +7,7 @@ import {
} from "../proto/server/zitadel/management";
import { authMiddleware } from "../middleware";
import { ZitadelApp, getApps } from "../app/app";
import { ZitadelServer, getServers } from "../server";
const createClient = <Client>(
definition: CompatServiceDefinition,
@@ -24,10 +24,10 @@ const createClient = <Client>(
.create(definition, channel) as Client;
};
export const getManagement = (app?: string | ZitadelApp) => {
export const getManagement = (app?: string | ZitadelServer) => {
let config;
if (app && typeof app === "string") {
const apps = getApps();
const apps = getServers();
config = apps.find((a) => a.name === app)?.config;
} else if (app && typeof app === "object") {
config = app.config;

View File

@@ -0,0 +1,35 @@
let apps: ZitadelServer[] = [];
export interface ZitadelServerProps {
apiUrl: string; // process.env.ZITADEL_API_URL
token: string;
}
export interface ZitadelServerOptions extends ZitadelServerProps {
name?: string;
}
export interface ZitadelServer {
name: string | undefined;
config: ZitadelServerProps;
}
export async function initializeServer(
config: ZitadelServerProps,
name?: string
): Promise<ZitadelServer> {
const app = { config, name };
return app;
}
export function getServers(): ZitadelServer[] {
return apps;
}
export function getServer(name?: string): ZitadelServer | undefined {
return name
? apps.find((a) => a.name === name)
: apps.length === 1
? apps[0]
: undefined;
}

View File

@@ -1,8 +1,12 @@
{
"extends": "@zitadel/tsconfig/node14.json",
"include": ["src/**/*"],
"include": ["src/**/*.ts"],
"compilerOptions": {
"baseUrl": "."
"baseUrl": ".",
"paths": {
"*": ["./src/*"],
"#/*": ["./src/*"]
}
},
"exclude": ["dist", "build", "node_modules"]
}

54
pnpm-lock.yaml generated
View File

@@ -14,7 +14,7 @@ importers:
eslint: 7.32.0
eslint-config-zitadel: link:packages/eslint-config-zitadel
prettier: 2.8.0
turbo: 1.9.1
turbo: 1.9.3
apps/login:
specifiers:
@@ -100,7 +100,7 @@ importers:
dependencies:
eslint-config-next: 13.3.0_hsf322ms6xhhd4b5ne6lb74y4a
eslint-config-prettier: 8.5.0_eslint@8.28.0
eslint-config-turbo: 1.9.1_eslint@8.28.0
eslint-config-turbo: 1.9.3_eslint@8.28.0
eslint-plugin-react: 7.28.0_eslint@8.28.0
packages/zitadel-client:
@@ -3556,13 +3556,13 @@ packages:
eslint: 8.28.0
dev: false
/eslint-config-turbo/1.9.1_eslint@8.28.0:
resolution: {integrity: sha512-tUqm5TxI5bpbDEgClbw+UygVPAwYB20FIpAiQsZI8imJNDz30E40TZkp6uWpAKmxykU8T0+t3jwkYokvXmXc0Q==}
/eslint-config-turbo/1.9.3_eslint@8.28.0:
resolution: {integrity: sha512-QG6jxFQkrGSpQqlFKefPdtgUfr20EbU0s4tGGIuGFOcPuJEdsY6VYZpZUxNJvmMcTGqPgMyOPjAFBKhy/DPHLA==}
peerDependencies:
eslint: '>6.6.0'
dependencies:
eslint: 8.28.0
eslint-plugin-turbo: 1.9.1_eslint@8.28.0
eslint-plugin-turbo: 1.9.3_eslint@8.28.0
dev: false
/eslint-import-resolver-node/0.3.6:
@@ -3730,8 +3730,8 @@ packages:
string.prototype.matchall: 4.0.8
dev: false
/eslint-plugin-turbo/1.9.1_eslint@8.28.0:
resolution: {integrity: sha512-QPd0EG0xkoDkXJLwPQKULxHjkR27VmvJtILW4C9aIrqauLZ+Yc/V7R+A9yVwAi6nkMHxUlCSUsBxmiQP9TIlPw==}
/eslint-plugin-turbo/1.9.3_eslint@8.28.0:
resolution: {integrity: sha512-ZsRtksdzk3v+z5/I/K4E50E4lfZ7oYmLX395gkrUMBz4/spJlYbr+GC8hP9oVNLj9s5Pvnm9rLv/zoj5PVYaVw==}
peerDependencies:
eslint: '>6.6.0'
dependencies:
@@ -6563,65 +6563,65 @@ packages:
yargs: 17.6.2
dev: true
/turbo-darwin-64/1.9.1:
resolution: {integrity: sha512-IX/Ph4CO80lFKd9pPx3BWpN2dynt6mcUFifyuHUNVkOP1Usza/G9YuZnKQFG6wUwKJbx40morFLjk1TTeLe04w==}
/turbo-darwin-64/1.9.3:
resolution: {integrity: sha512-0dFc2cWXl82kRE4Z+QqPHhbEFEpUZho1msHXHWbz5+PqLxn8FY0lEVOHkq5tgKNNEd5KnGyj33gC/bHhpZOk5g==}
cpu: [x64]
os: [darwin]
requiresBuild: true
dev: true
optional: true
/turbo-darwin-arm64/1.9.1:
resolution: {integrity: sha512-6tCbmIboy9dTbhIZ/x9KIpje73nvxbiyVnHbr9xKnsxLJavD0xqjHZzbL5U2tHp8chqmYf0E4WYOXd+XCNg+OQ==}
/turbo-darwin-arm64/1.9.3:
resolution: {integrity: sha512-1cYbjqLBA2zYE1nbf/qVnEkrHa4PkJJbLo7hnuMuGM0bPzh4+AnTNe98gELhqI1mkTWBu/XAEeF5u6dgz0jLNA==}
cpu: [arm64]
os: [darwin]
requiresBuild: true
dev: true
optional: true
/turbo-linux-64/1.9.1:
resolution: {integrity: sha512-ti8XofnJFO1XaadL92lYJXgxb0VBl03Yu9VfhxkOTywFe7USTLBkJcdvQ4EpFk/KZwLiTdCmT2NQVxsG4AxBiQ==}
/turbo-linux-64/1.9.3:
resolution: {integrity: sha512-UuBPFefawEwpuxh5pM9Jqq3q4C8M0vYxVYlB3qea/nHQ80pxYq7ZcaLGEpb10SGnr3oMUUs1zZvkXWDNKCJb8Q==}
cpu: [x64]
os: [linux]
requiresBuild: true
dev: true
optional: true
/turbo-linux-arm64/1.9.1:
resolution: {integrity: sha512-XYvIbeiCCCr+ENujd2Jtck/lJPTKWb8T2MSL/AEBx21Zy3Sa7HgrQX6LX0a0pNHjaleHz00XXt1D0W5hLeP+tA==}
/turbo-linux-arm64/1.9.3:
resolution: {integrity: sha512-vUrNGa3hyDtRh9W0MkO+l1dzP8Co2gKnOVmlJQW0hdpOlWlIh22nHNGGlICg+xFa2f9j4PbQlWTsc22c019s8Q==}
cpu: [arm64]
os: [linux]
requiresBuild: true
dev: true
optional: true
/turbo-windows-64/1.9.1:
resolution: {integrity: sha512-x7lWAspe4/v3XQ0gaFRWDX/X9uyWdhwFBPEfb8BA0YKtnsrPOHkV0mRHCRrXzvzjA7pcDCl2agGzb7o863O+Jg==}
/turbo-windows-64/1.9.3:
resolution: {integrity: sha512-0BZ7YaHs6r+K4ksqWus1GKK3W45DuDqlmfjm/yuUbTEVc8szmMCs12vugU2Zi5GdrdJSYfoKfEJ/PeegSLIQGQ==}
cpu: [x64]
os: [win32]
requiresBuild: true
dev: true
optional: true
/turbo-windows-arm64/1.9.1:
resolution: {integrity: sha512-QSLNz8dRBLDqXOUv/KnoesBomSbIz2Huef/a3l2+Pat5wkQVgMfzFxDOnkK5VWujPYXz+/prYz+/7cdaC78/kw==}
/turbo-windows-arm64/1.9.3:
resolution: {integrity: sha512-QJUYLSsxdXOsR1TquiOmLdAgtYcQ/RuSRpScGvnZb1hY0oLc7JWU0llkYB81wVtWs469y8H9O0cxbKwCZGR4RQ==}
cpu: [arm64]
os: [win32]
requiresBuild: true
dev: true
optional: true
/turbo/1.9.1:
resolution: {integrity: sha512-Rqe8SP96e53y4Pk29kk2aZbA8EF11UtHJ3vzXJseadrc1T3V6UhzvAWwiKJL//x/jojyOoX1axnoxmX3UHbZ0g==}
/turbo/1.9.3:
resolution: {integrity: sha512-ID7mxmaLUPKG/hVkp+h0VuucB1U99RPCJD9cEuSEOdIPoSIuomcIClEJtKamUsdPLhLCud+BvapBNnhgh58Nzw==}
hasBin: true
requiresBuild: true
optionalDependencies:
turbo-darwin-64: 1.9.1
turbo-darwin-arm64: 1.9.1
turbo-linux-64: 1.9.1
turbo-linux-arm64: 1.9.1
turbo-windows-64: 1.9.1
turbo-windows-arm64: 1.9.1
turbo-darwin-64: 1.9.3
turbo-darwin-arm64: 1.9.3
turbo-linux-64: 1.9.3
turbo-linux-arm64: 1.9.3
turbo-windows-64: 1.9.3
turbo-windows-arm64: 1.9.3
dev: true
/type-check/0.4.0:

View File

@@ -22,5 +22,10 @@
}
},
"globalDependencies": ["**/.env.*local"],
"globalEnv": ["ZITADEL_API_URL", "ZITADEL_PROJECT_ID", "ZITADEL_APP_ID"]
"globalEnv": [
"ZITADEL_API_URL",
"ZITADEL_PROJECT_ID",
"ZITADEL_APP_ID",
"ZITADEL_SERVICE_USER_TOKEN"
]
}