@cripty2001/utils 0.0.244 → 0.0.246

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,41 +1 @@
1
- import { Whispr } from "@cripty2001/whispr";
2
- import { AppserverData } from "./common";
3
- export type { AppserverData };
4
- export type ClientCreateOptions = {
5
- rpcMount: string;
6
- };
7
- export declare class ClientError extends Error {
8
- constructor(message: string);
9
- }
10
- export declare class ClientAuthError extends ClientError {
11
- readonly code: string;
12
- readonly payload: AppserverData;
13
- constructor(code?: string, message?: string, payload?: AppserverData);
14
- }
15
- export declare class ClientServerError extends ClientError {
16
- code: string;
17
- payload: AppserverData;
18
- constructor(code: string, message: string, payload?: AppserverData);
19
- }
20
- type ClientValidationErrorError = {
21
- path: string;
22
- message: string;
23
- value: unknown;
24
- };
25
- export declare class ClientValidationError extends ClientError {
26
- readonly errors: ClientValidationErrorError[];
27
- constructor(errors: ClientValidationErrorError[]);
28
- }
29
- export declare class Client {
30
- private url;
31
- private readonly rpcMount;
32
- private readonly token;
33
- private readonly setToken;
34
- readonly loggedIn: Whispr<boolean>;
35
- private constructor();
36
- static create(url: string, options: ClientCreateOptions): Client;
37
- login(token: string): Promise<boolean>;
38
- logout(): void;
39
- exec<I extends AppserverData, O extends AppserverData>(action: string, input: I, onError?: Record<string, (payload: AppserverData) => Promise<void>>): Promise<O>;
40
- private unsafeExec;
41
- }
1
+ export {};
@@ -1,127 +1,147 @@
1
1
  "use strict";
2
+ // import { Whispr, WhisprSetter } from "@cripty2001/whispr";
3
+ // import { decode, encode } from "@msgpack/msgpack";
4
+ // import { AppserverData } from "./common";
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Client = exports.ClientValidationError = exports.ClientServerError = exports.ClientAuthError = exports.ClientError = void 0;
4
- const whispr_1 = require("@cripty2001/whispr");
5
- const msgpack_1 = require("@msgpack/msgpack");
6
- class ClientError extends Error {
7
- constructor(message) {
8
- super(message);
9
- }
10
- }
11
- exports.ClientError = ClientError;
12
- class ClientAuthError extends ClientError {
13
- code;
14
- payload;
15
- constructor(code = "PERMISSION_DENIED", message = "Permission denied", payload = {}) {
16
- super(message);
17
- this.code = code;
18
- this.payload = payload;
19
- }
20
- }
21
- exports.ClientAuthError = ClientAuthError;
22
- class ClientServerError extends ClientError {
23
- code;
24
- payload;
25
- constructor(code, message, payload = {}) {
26
- super(message);
27
- this.code = code;
28
- this.payload = payload;
29
- }
30
- }
31
- exports.ClientServerError = ClientServerError;
32
- class ClientValidationError extends ClientError {
33
- errors;
34
- constructor(errors) {
35
- const first = errors[0] ?? {
36
- path: "Internal Error",
37
- message: "",
38
- value: undefined,
39
- };
40
- super(`[Validation Error] ${first.path}: ${first.message}`);
41
- this.errors = errors;
42
- }
43
- }
44
- exports.ClientValidationError = ClientValidationError;
45
- class Client {
46
- url;
47
- rpcMount;
48
- token;
49
- setToken;
50
- loggedIn;
51
- constructor(url, rpcMount) {
52
- this.url = url;
53
- this.rpcMount = rpcMount;
54
- const [token, setToken] = whispr_1.Whispr.create(''); // This will make sure the loggedin only flips to false when there is really an auth rejected. Server will ignore invalid tokens on public apis anyway
55
- this.token = token;
56
- this.setToken = setToken;
57
- this.loggedIn = token.transform((t) => t !== null);
58
- }
59
- static create(url, options) {
60
- const m = options.rpcMount.startsWith("/") ?
61
- options.rpcMount :
62
- `/${options.rpcMount}`;
63
- return new Client(url, m);
64
- }
65
- async login(token) {
66
- this.setToken(token);
67
- return true;
68
- }
69
- logout() {
70
- this.setToken(null);
71
- }
72
- async exec(action, input, onError = {}) {
73
- while (true) {
74
- try {
75
- return await this.unsafeExec(action, input);
76
- }
77
- catch (e) {
78
- if (e instanceof ClientAuthError) {
79
- this.logout();
80
- await this.token.load();
81
- continue;
82
- }
83
- if (e instanceof ClientServerError) {
84
- const handler = onError[e.code];
85
- if (handler) {
86
- await handler(e.payload);
87
- }
88
- }
89
- throw e;
90
- }
91
- }
92
- }
93
- async unsafeExec(action, input) {
94
- const testedToken = this.token.value;
95
- const actionKey = action;
96
- const wireBody = {
97
- action: actionKey,
98
- payload: input,
99
- };
100
- const res = await fetch(`${this.url}${this.rpcMount}`, {
101
- method: "POST",
102
- headers: {
103
- "Content-Type": "application/vnd.msgpack",
104
- ...(testedToken !== null ? { Authorization: `Bearer ${testedToken}` } : {}),
105
- },
106
- body: new Uint8Array((0, msgpack_1.encode)(wireBody)),
107
- });
108
- const buf = await res.arrayBuffer();
109
- const decoded = (0, msgpack_1.decode)(buf);
110
- if (res.status === 200)
111
- return decoded;
112
- if (res.status === 422) {
113
- const data = decoded;
114
- throw new ClientValidationError(data.errors);
115
- }
116
- const genericError = decoded;
117
- if (res.status === 400 || res.status === 404 || res.status === 500) {
118
- throw new ClientServerError(genericError.code, genericError.error, genericError.payload ?? {});
119
- }
120
- if (res.status === 401 || res.status === 403) {
121
- this.setToken(null);
122
- throw new ClientAuthError(genericError.code, genericError.error, genericError.payload);
123
- }
124
- throw new ClientError(`Unexpected server response: ${res.status}`);
125
- }
126
- }
127
- exports.Client = Client;
6
+ // export type { AppserverData };
7
+ // export type ClientCreateOptions = {
8
+ // rpcMount: string;
9
+ // };
10
+ // export class ClientError extends Error {
11
+ // constructor(message: string) {
12
+ // super(message);
13
+ // }
14
+ // }
15
+ // export class ClientAuthError extends ClientError {
16
+ // constructor(
17
+ // public readonly code: string = "PERMISSION_DENIED",
18
+ // message: string = "Permission denied",
19
+ // public readonly payload: AppserverData = {},
20
+ // ) {
21
+ // super(message);
22
+ // }
23
+ // }
24
+ // export class ClientServerError extends ClientError {
25
+ // constructor(public code: string, message: string, public payload: AppserverData = {}) {
26
+ // super(message);
27
+ // }
28
+ // }
29
+ // type ClientValidationErrorError = {
30
+ // path: string;
31
+ // message: string;
32
+ // value: unknown;
33
+ // }
34
+ // export class ClientValidationError extends ClientError {
35
+ // public readonly errors: ClientValidationErrorError[];
36
+ // constructor(errors: ClientValidationErrorError[]) {
37
+ // const first = errors[0] ?? {
38
+ // path: "Internal Error",
39
+ // message: "",
40
+ // value: undefined,
41
+ // };
42
+ // super(`[Validation Error] ${first.path}: ${first.message}`);
43
+ // this.errors = errors;
44
+ // }
45
+ // }
46
+ // export class Client {
47
+ // private readonly token: Whispr<string | null>;
48
+ // private readonly setToken: WhisprSetter<string | null>;
49
+ // public readonly loggedIn: Whispr<boolean>;
50
+ // private constructor(
51
+ // private url: string,
52
+ // private readonly rpcMount: string,
53
+ // ) {
54
+ // const [token, setToken] = Whispr.create<string | null>(''); // This will make sure the loggedin only flips to false when there is really an auth rejected. Server will ignore invalid tokens on public apis anyway
55
+ // this.token = token;
56
+ // this.setToken = setToken;
57
+ // this.loggedIn = token.transform((t) => t !== null);
58
+ // }
59
+ // public static create(url: string, options: ClientCreateOptions): Client {
60
+ // const m = options.rpcMount.startsWith("/") ?
61
+ // options.rpcMount :
62
+ // `/${options.rpcMount}`;
63
+ // return new Client(url, m);
64
+ // }
65
+ // public async login(token: string): Promise<boolean> {
66
+ // this.setToken(token);
67
+ // return true;
68
+ // }
69
+ // public logout(): void {
70
+ // this.setToken(null);
71
+ // }
72
+ // public async exec<I extends AppserverData, O extends AppserverData>(
73
+ // action: string,
74
+ // input: I,
75
+ // onError: Record<string, (payload: AppserverData) => Promise<void>> = {},
76
+ // ): Promise<O> {
77
+ // while (true) {
78
+ // try {
79
+ // return await this.unsafeExec(action, input);
80
+ // } catch (e) {
81
+ // if (e instanceof ClientAuthError) {
82
+ // this.logout();
83
+ // await this.token.load();
84
+ // continue;
85
+ // }
86
+ // if (e instanceof ClientServerError) {
87
+ // const handler = onError[e.code];
88
+ // if (handler) {
89
+ // await handler(e.payload);
90
+ // }
91
+ // }
92
+ // throw e;
93
+ // }
94
+ // }
95
+ // }
96
+ // private async unsafeExec<I extends AppserverData, O extends AppserverData>(
97
+ // action: string,
98
+ // input: I,
99
+ // ): Promise<O> {
100
+ // const testedToken = this.token.value;
101
+ // const actionKey = action;
102
+ // const wireBody = {
103
+ // action: actionKey,
104
+ // payload: input as AppserverData,
105
+ // };
106
+ // const res = await fetch(`${this.url}${this.rpcMount}`, {
107
+ // method: "POST",
108
+ // headers: {
109
+ // "Content-Type": "application/vnd.msgpack",
110
+ // ...(testedToken !== null ? { Authorization: `Bearer ${testedToken}` } : {}),
111
+ // },
112
+ // body: new Uint8Array(encode(wireBody)),
113
+ // });
114
+ // const buf = await res.arrayBuffer();
115
+ // const decoded = decode(buf);
116
+ // if (res.status === 200)
117
+ // return decoded as O;
118
+ // if (res.status === 422) {
119
+ // const data = decoded as {
120
+ // errors: ClientValidationErrorError[];
121
+ // received: AppserverData;
122
+ // }
123
+ // throw new ClientValidationError(data.errors);
124
+ // }
125
+ // const genericError = decoded as {
126
+ // error: string;
127
+ // code: string;
128
+ // payload: AppserverData;
129
+ // }
130
+ // if (res.status === 400 || res.status === 404 || res.status === 500) {
131
+ // throw new ClientServerError(
132
+ // genericError.code,
133
+ // genericError.error,
134
+ // genericError.payload ?? {},
135
+ // );
136
+ // }
137
+ // if (res.status === 401 || res.status === 403) {
138
+ // this.setToken(null);
139
+ // throw new ClientAuthError(
140
+ // genericError.code,
141
+ // genericError.error,
142
+ // genericError.payload,
143
+ // )
144
+ // }
145
+ // throw new ClientError(`Unexpected server response: ${res.status}`);
146
+ // }
147
+ // }
@@ -1,31 +1,41 @@
1
- import { Static, TSchema } from '@sinclair/typebox';
2
- import express from 'express';
3
- import { AppserverData } from './common';
4
- export type { AppserverData };
5
- export { Static, TSchema, Type } from '@sinclair/typebox';
6
- export { Value } from '@sinclair/typebox/value';
7
- export { FormatRegistry } from '@sinclair/typebox';
8
- export type AppserverHandler<I extends AppserverData, O extends AppserverData> = (input: I, auth: string | undefined) => Promise<O> | O;
9
- declare class AppserverError<P extends AppserverData = AppserverData> extends Error {
10
- code: string;
11
- payload: P;
12
- status: number;
13
- constructor(code: string, message: string, payload?: P, status?: number);
1
+ import { type Static, type TSchema, type TypeBoxTypeProvider, Type } from '@fastify/type-provider-typebox';
2
+ export { Type };
3
+ declare module 'fastify' {
4
+ interface FastifyRequest {
5
+ user: {
6
+ id: string;
7
+ } | null;
8
+ }
14
9
  }
15
- export declare class AppserverAuthError extends AppserverError {
16
- constructor(message?: string, code?: string);
10
+ export declare class Appserver {
11
+ readonly fastify: import("fastify").FastifyInstance<import("http").Server<typeof import("http").IncomingMessage, typeof import("http").ServerResponse>, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").FastifyBaseLogger, TypeBoxTypeProvider>;
12
+ private readonly jwks;
13
+ private readonly issuer;
14
+ private readonly audience;
15
+ private static resolveAuth;
16
+ private readonly rpcBase;
17
+ private constructor();
18
+ static create(audience: string, issuerUrl: string, rpcBase?: string): Promise<Appserver>;
19
+ listen(port: number): Promise<void>;
20
+ registerRpc<TInput extends TSchema, TOutput extends TSchema>(route: string, schema: {
21
+ input: TInput;
22
+ output: TOutput;
23
+ }, scope: string, handler: (data: Static<TInput>, user: {
24
+ id: string;
25
+ }) => Promise<Static<TOutput>>): void;
26
+ private enforceAccess;
27
+ private handleError;
28
+ autoload(base: string): Promise<void>;
17
29
  }
18
- export declare class AppserverHandledError<P extends AppserverData = AppserverData> extends AppserverError<P> {
19
- constructor(code: string, message: string, payload?: P);
30
+ declare class AppserverHandledError<T extends TSchema = Record<string, never>> extends Error {
31
+ readonly status: number;
32
+ readonly code: string;
33
+ readonly details: T;
34
+ constructor(code: string, message: string, status: number, details: T);
20
35
  }
21
- export type AppserverModule<ISchema extends TSchema = TSchema, O extends AppserverData = AppserverData, I extends Static<ISchema> & AppserverData = Static<ISchema> & AppserverData> = {
22
- schema: ISchema;
23
- handler: AppserverHandler<I, O>;
24
- };
25
- export declare class AppserverDispatcher {
26
- private registry;
27
- register<ISchema extends TSchema, O extends AppserverData, I extends Static<ISchema> & AppserverData = Static<ISchema> & AppserverData>(action: string, mod: AppserverModule<ISchema, O, I>): void;
28
- autoload(base: string): Promise<void>;
29
- bind(app: express.Express, path: string): void;
30
- private dispatch;
36
+ export declare class AppserverAuthHandledError extends AppserverHandledError {
37
+ constructor(code: string, message: string, recoverable?: boolean);
38
+ }
39
+ export declare class AppserverUserHandledError<T extends TSchema = Record<string, never>> extends AppserverHandledError<T> {
40
+ constructor(code: string, message: string, details: T);
31
41
  }