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

@@ -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"]
}