@pikku/core 0.7.0 → 0.7.1
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.
- package/CHANGELOG.md +6 -0
- package/dist/channel/channel-runner.js +1 -1
- package/dist/channel/serverless/serverless-channel-runner.js +1 -1
- package/dist/http/http-runner.js +4 -2
- package/dist/http/log-http-routes.js +1 -1
- package/lcov.info +4585 -0
- package/package.json +1 -1
- package/src/channel/channel-handler.ts +165 -0
- package/src/channel/channel-runner.ts +153 -0
- package/src/channel/channel-store.ts +29 -0
- package/src/channel/channel.types.ts +133 -0
- package/src/channel/eventhub-service.ts +30 -0
- package/src/channel/eventhub-store.ts +8 -0
- package/src/channel/index.ts +8 -0
- package/src/channel/local/index.ts +3 -0
- package/src/channel/local/local-channel-handler.ts +50 -0
- package/src/channel/local/local-channel-runner.test.ts +157 -0
- package/src/channel/local/local-channel-runner.ts +139 -0
- package/src/channel/local/local-eventhub-service.test.ts +95 -0
- package/src/channel/local/local-eventhub-service.ts +95 -0
- package/src/channel/log-channels.ts +20 -0
- package/src/channel/pikku-abstract-channel-handler.test.ts +52 -0
- package/src/channel/pikku-abstract-channel-handler.ts +38 -0
- package/src/channel/serverless/index.ts +1 -0
- package/src/channel/serverless/serverless-channel-runner.ts +220 -0
- package/src/errors/error-handler.ts +61 -0
- package/src/errors/error.test.ts +54 -0
- package/src/errors/errors.ts +339 -0
- package/src/errors/index.ts +2 -0
- package/src/function/function-runner.ts +91 -0
- package/src/function/functions.types.ts +77 -0
- package/src/function/index.ts +2 -0
- package/src/handle-error.ts +73 -0
- package/src/http/http-runner.test.ts +140 -0
- package/src/http/http-runner.ts +493 -0
- package/src/http/http.types.ts +250 -0
- package/src/http/incomingmessage-to-request-convertor.ts +0 -0
- package/src/http/index.ts +8 -0
- package/src/http/log-http-routes.ts +22 -0
- package/src/http/pikku-fetch-http-request.test.ts +237 -0
- package/src/http/pikku-fetch-http-request.ts +163 -0
- package/src/http/pikku-fetch-http-response.test.ts +82 -0
- package/src/http/pikku-fetch-http-response.ts +138 -0
- package/src/index.ts +20 -0
- package/src/middleware/auth-apikey.ts +66 -0
- package/src/middleware/auth-bearer.ts +66 -0
- package/src/middleware/auth-cookie.ts +103 -0
- package/src/middleware/index.ts +3 -0
- package/src/middleware/timeout.ts +13 -0
- package/src/middleware-runner.ts +43 -0
- package/src/permissions.test.ts +58 -0
- package/src/permissions.ts +43 -0
- package/src/pikku-function.ts +1 -0
- package/src/pikku-request.ts +23 -0
- package/src/pikku-response.ts +5 -0
- package/src/pikku-state.ts +87 -0
- package/src/scheduler/index.ts +5 -0
- package/src/scheduler/log-schedulers.ts +20 -0
- package/src/scheduler/scheduler-runner.ts +105 -0
- package/src/scheduler/scheduler.types.ts +33 -0
- package/src/schema.test.ts +57 -0
- package/src/schema.ts +99 -0
- package/src/services/content-service.ts +68 -0
- package/src/services/index.ts +18 -0
- package/src/services/jwt-service.ts +30 -0
- package/src/services/local-content.ts +105 -0
- package/src/services/local-secrets.ts +43 -0
- package/src/services/local-variables.ts +17 -0
- package/src/services/logger-console.ts +97 -0
- package/src/services/logger.ts +57 -0
- package/src/services/schema-service.ts +26 -0
- package/src/services/secret-service.ts +17 -0
- package/src/services/user-session-service.ts +53 -0
- package/src/services/variables-service.ts +6 -0
- package/src/time-utils.test.ts +56 -0
- package/src/time-utils.ts +32 -0
- package/src/types/core.types.ts +178 -0
- package/src/utils.ts +40 -0
- package/tsconfig.json +13 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/dist/http/http-route-runner.d.ts +0 -90
- package/dist/http/http-route-runner.js +0 -325
- package/dist/http/http-routes.types.d.ts +0 -179
- package/dist/http/http-routes.types.js +0 -1
- package/dist/http/pikku-http-request.d.ts +0 -56
- package/dist/http/pikku-http-request.js +0 -95
- package/dist/http/pikku-http-response.d.ts +0 -13
- package/dist/http/pikku-http-response.js +0 -60
- package/dist/parse-relative-time-offset.d.ts +0 -12
- package/dist/parse-relative-time-offset.js +0 -20
- package/dist/pikku-func.d.ts +0 -12
- package/dist/pikku-func.js +0 -39
- package/dist/types/functions.types.d.ts +0 -37
- package/dist/types/functions.types.js +0 -1
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
import type { SerializeOptions } from 'cookie';
|
|
2
|
-
import type { PikkuError } from '../errors/error-handler.js';
|
|
3
|
-
import type { APIDocs, CoreServices, CoreSingletonServices, CoreUserSession, CreateSessionServices, PikkuMiddleware } from '../types/core.types.js';
|
|
4
|
-
import type { CoreAPIFunction, CoreAPIFunctionSessionless, CoreAPIPermission, CorePermissionGroup } from '../function/functions.types.js';
|
|
5
|
-
type ExtractRouteParams<S extends string> = S extends `${string}:${infer Param}/${infer Rest}` ? Param | ExtractRouteParams<`/${Rest}`> : S extends `${string}:${infer Param}` ? Param : never;
|
|
6
|
-
export type AssertRouteParams<In, Route extends string> = ExtractRouteParams<Route> extends keyof In ? unknown : ['Error: Route parameters', ExtractRouteParams<Route>, 'not in', keyof In];
|
|
7
|
-
export type RunRouteOptions = Partial<{
|
|
8
|
-
skipUserSession: boolean;
|
|
9
|
-
respondWith404: boolean;
|
|
10
|
-
logWarningsForStatusCodes: number[];
|
|
11
|
-
coerceDataFromSchema: boolean;
|
|
12
|
-
bubbleErrors: boolean;
|
|
13
|
-
generateRequestId: () => string;
|
|
14
|
-
}>;
|
|
15
|
-
export type RunRouteParams = {
|
|
16
|
-
singletonServices: CoreSingletonServices;
|
|
17
|
-
createSessionServices: CreateSessionServices<CoreSingletonServices, CoreServices<CoreSingletonServices>, CoreUserSession>;
|
|
18
|
-
};
|
|
19
|
-
/**
|
|
20
|
-
* Represents the HTTP methods supported for API routes.
|
|
21
|
-
*/
|
|
22
|
-
export type HTTPMethod = 'post' | 'get' | 'delete' | 'patch' | 'head' | 'put' | 'options';
|
|
23
|
-
/**
|
|
24
|
-
* Represents an API route without a function, including metadata such as content type, route, and timeout settings.
|
|
25
|
-
*/
|
|
26
|
-
export type CoreHTTPFunction = {
|
|
27
|
-
contentType?: 'xml' | 'json';
|
|
28
|
-
route: string;
|
|
29
|
-
eventChannel?: false;
|
|
30
|
-
returnsJSON?: false;
|
|
31
|
-
timeout?: number;
|
|
32
|
-
docs?: Partial<{
|
|
33
|
-
description: string;
|
|
34
|
-
response: string;
|
|
35
|
-
errors: Array<typeof PikkuError>;
|
|
36
|
-
tags: string[];
|
|
37
|
-
}>;
|
|
38
|
-
};
|
|
39
|
-
/**
|
|
40
|
-
* Represents a http interaction within Pikku, including a request and response.
|
|
41
|
-
*/
|
|
42
|
-
export interface PikkuHTTP {
|
|
43
|
-
request?: PikkuHTTPRequest;
|
|
44
|
-
response?: PikkuHTTPResponse;
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Represents request headers as either a record or a function to get headers by name.
|
|
48
|
-
*/
|
|
49
|
-
export type RequestHeaders = Record<string, string | string[] | undefined> | ((headerName: string) => string | string[] | undefined);
|
|
50
|
-
/**
|
|
51
|
-
* Represents a query object for Pikku, where each key can be a string, a value, or an array of values.
|
|
52
|
-
*/
|
|
53
|
-
export type PikkuQuery<T = Record<string, string | undefined>> = Record<string, string | T | null | Array<T | null>>;
|
|
54
|
-
/**
|
|
55
|
-
* Represents a core API route, which can have different configurations depending on whether it requires authentication and permissions.
|
|
56
|
-
*
|
|
57
|
-
* @template In - The input type.
|
|
58
|
-
* @template Out - The output type.
|
|
59
|
-
* @template R - The route string type.
|
|
60
|
-
* @template APIFunction - The API function type, defaults to `CoreAPIFunction`.
|
|
61
|
-
* @template APIFunctionSessionless - The sessionless API function type, defaults to `CoreAPIFunctionSessionless`.
|
|
62
|
-
* @template APIPermission - The permission function type, defaults to `CoreAPIPermission`.
|
|
63
|
-
*/
|
|
64
|
-
export type CoreHTTPFunctionRoute<In, Out, R extends string, APIFunction = CoreAPIFunction<In, Out>, APIFunctionSessionless = CoreAPIFunctionSessionless<In, Out>, APIPermission = CoreAPIPermission<In>, APIMiddleware = PikkuMiddleware> = (CoreHTTPFunction & {
|
|
65
|
-
route: R;
|
|
66
|
-
method: HTTPMethod;
|
|
67
|
-
func: APIFunction;
|
|
68
|
-
permissions?: CorePermissionGroup<APIPermission>;
|
|
69
|
-
auth?: true;
|
|
70
|
-
tags?: string[];
|
|
71
|
-
middleware?: APIMiddleware[];
|
|
72
|
-
sse?: undefined;
|
|
73
|
-
}) | (CoreHTTPFunction & {
|
|
74
|
-
route: R;
|
|
75
|
-
method: HTTPMethod;
|
|
76
|
-
func: APIFunctionSessionless;
|
|
77
|
-
permissions?: undefined;
|
|
78
|
-
auth?: false;
|
|
79
|
-
tags?: string[];
|
|
80
|
-
middleware?: APIMiddleware[];
|
|
81
|
-
sse?: undefined;
|
|
82
|
-
}) | (CoreHTTPFunction & {
|
|
83
|
-
route: R;
|
|
84
|
-
method: 'get';
|
|
85
|
-
func: APIFunction;
|
|
86
|
-
permissions?: CorePermissionGroup<APIPermission>;
|
|
87
|
-
auth?: true;
|
|
88
|
-
sse?: boolean;
|
|
89
|
-
tags?: string[];
|
|
90
|
-
middleware?: APIMiddleware[];
|
|
91
|
-
}) | (CoreHTTPFunction & {
|
|
92
|
-
route: R;
|
|
93
|
-
method: 'get';
|
|
94
|
-
func: APIFunctionSessionless;
|
|
95
|
-
permissions?: undefined;
|
|
96
|
-
auth?: false;
|
|
97
|
-
sse?: boolean;
|
|
98
|
-
tags?: string[];
|
|
99
|
-
middleware?: APIMiddleware[];
|
|
100
|
-
}) | (CoreHTTPFunction & {
|
|
101
|
-
route: R;
|
|
102
|
-
method: 'post';
|
|
103
|
-
func: APIFunction;
|
|
104
|
-
permissions?: CorePermissionGroup<APIPermission>;
|
|
105
|
-
auth?: true;
|
|
106
|
-
query?: Array<keyof In>;
|
|
107
|
-
tags?: string[];
|
|
108
|
-
middleware?: APIMiddleware[];
|
|
109
|
-
sse?: undefined;
|
|
110
|
-
}) | (CoreHTTPFunction & {
|
|
111
|
-
route: R;
|
|
112
|
-
method: 'post';
|
|
113
|
-
func: APIFunctionSessionless;
|
|
114
|
-
permissions?: undefined;
|
|
115
|
-
auth?: false;
|
|
116
|
-
query?: Array<keyof In>;
|
|
117
|
-
tags?: string[];
|
|
118
|
-
middleware?: APIMiddleware[];
|
|
119
|
-
sse?: undefined;
|
|
120
|
-
});
|
|
121
|
-
/**
|
|
122
|
-
* Represents an array of core API routes.
|
|
123
|
-
*/
|
|
124
|
-
export type CoreHTTPFunctionRoutes = Array<CoreHTTPFunctionRoute<any, any, string>>;
|
|
125
|
-
/**
|
|
126
|
-
* Represents the input types for route metadata, including parameters, query, and body types.
|
|
127
|
-
*/
|
|
128
|
-
export type HTTPFunctionMetaInputTypes = {
|
|
129
|
-
params?: string;
|
|
130
|
-
query?: string;
|
|
131
|
-
body?: string;
|
|
132
|
-
};
|
|
133
|
-
/**
|
|
134
|
-
* Represents metadata for a set of routes, including route details, methods, input/output types, and documentation.
|
|
135
|
-
*/
|
|
136
|
-
export type HTTPRoutesMeta = Array<{
|
|
137
|
-
route: string;
|
|
138
|
-
method: HTTPMethod;
|
|
139
|
-
params?: string[];
|
|
140
|
-
query?: string[];
|
|
141
|
-
input: string | null;
|
|
142
|
-
output: string | null;
|
|
143
|
-
inputTypes?: HTTPFunctionMetaInputTypes;
|
|
144
|
-
docs?: APIDocs;
|
|
145
|
-
tags?: string[];
|
|
146
|
-
sse?: true;
|
|
147
|
-
}>;
|
|
148
|
-
export type HTTPFunctionsMeta = Array<{
|
|
149
|
-
name: string;
|
|
150
|
-
inputs: string[] | null;
|
|
151
|
-
outputs: string[] | null;
|
|
152
|
-
}>;
|
|
153
|
-
export type HTTPRouteMiddleware = {
|
|
154
|
-
route: string;
|
|
155
|
-
middleware: PikkuMiddleware[];
|
|
156
|
-
};
|
|
157
|
-
export interface PikkuHTTPRequest<In = unknown> {
|
|
158
|
-
method(): HTTPMethod;
|
|
159
|
-
path(): string;
|
|
160
|
-
data(): Promise<In>;
|
|
161
|
-
json(): Promise<unknown>;
|
|
162
|
-
arrayBuffer(): Promise<ArrayBuffer>;
|
|
163
|
-
header(headerName: string): string | null;
|
|
164
|
-
cookie(name?: string): string | null;
|
|
165
|
-
params(): Partial<Record<string, string | string[]>>;
|
|
166
|
-
setParams(params: Record<string, string | string[] | undefined>): void;
|
|
167
|
-
query(): PikkuQuery;
|
|
168
|
-
}
|
|
169
|
-
export interface PikkuHTTPResponse {
|
|
170
|
-
status(code: number): this;
|
|
171
|
-
cookie(name: string, value: string | null, options: SerializeOptions): this;
|
|
172
|
-
header(name: string, value: string | string[]): this;
|
|
173
|
-
arrayBuffer(data: ArrayBuffer | ArrayBufferView | Blob | string | FormData | URLSearchParams | ReadableStream): this;
|
|
174
|
-
json(data: unknown): this;
|
|
175
|
-
redirect(location: string, status?: number): this;
|
|
176
|
-
close?: () => void;
|
|
177
|
-
setMode?: (mode: 'stream') => void;
|
|
178
|
-
}
|
|
179
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { PikkuRequest } from '../pikku-request.js';
|
|
2
|
-
import { HTTPMethod, PikkuQuery } from './http-routes.types.js';
|
|
3
|
-
/**
|
|
4
|
-
* Abstract class representing a pikku request.
|
|
5
|
-
* @template In - The type of the request body.
|
|
6
|
-
* @group RequestResponse
|
|
7
|
-
*/
|
|
8
|
-
export declare class PikkuHTTPRequest<In = unknown> extends PikkuRequest<In> {
|
|
9
|
-
private request;
|
|
10
|
-
private _params;
|
|
11
|
-
private url;
|
|
12
|
-
constructor(request: Request);
|
|
13
|
-
method(): HTTPMethod;
|
|
14
|
-
path(): string;
|
|
15
|
-
/**
|
|
16
|
-
* Retrieves the request body.
|
|
17
|
-
* @returns A promise that resolves to the request body.
|
|
18
|
-
*/
|
|
19
|
-
json(): Promise<In>;
|
|
20
|
-
/**
|
|
21
|
-
* Retrieves the raw request body as a Buffer.
|
|
22
|
-
* @returns A promise that resolves to the raw request body.
|
|
23
|
-
*/
|
|
24
|
-
arrayBuffer(): Promise<ArrayBuffer>;
|
|
25
|
-
/**
|
|
26
|
-
* Retrieves the value of a specific header.
|
|
27
|
-
* @param headerName - The name of the header to retrieve.
|
|
28
|
-
* @returns The value of the header, or undefined if the header is not found.
|
|
29
|
-
*/
|
|
30
|
-
header(headerName: string): string | null;
|
|
31
|
-
/**
|
|
32
|
-
* Retrieves the cookies from the request.
|
|
33
|
-
* @returns An object containing the cookies.
|
|
34
|
-
*/
|
|
35
|
-
cookies(): Partial<Record<string, string>>;
|
|
36
|
-
/**
|
|
37
|
-
* Retrieves the request parameters.
|
|
38
|
-
* @returns An object containing the request parameters.
|
|
39
|
-
*/
|
|
40
|
-
params(): Partial<Record<string, string | string[]>>;
|
|
41
|
-
/**
|
|
42
|
-
* Sets the request parameters.
|
|
43
|
-
* @param params - An object containing the request parameters to set.
|
|
44
|
-
*/
|
|
45
|
-
setParams(params: Record<string, string | string[] | undefined>): void;
|
|
46
|
-
/**
|
|
47
|
-
* Retrieves the query parameters from the request.
|
|
48
|
-
* @returns An object containing the query parameters.
|
|
49
|
-
*/
|
|
50
|
-
query(): PikkuQuery;
|
|
51
|
-
/**
|
|
52
|
-
* Retrieves the combined data from the request, including parameters, query, and body.
|
|
53
|
-
* @returns A promise that resolves to an object containing the combined data.
|
|
54
|
-
*/
|
|
55
|
-
getData(): Promise<In>;
|
|
56
|
-
}
|
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
import { parse as parseQuery } from 'picoquery';
|
|
2
|
-
import { parse as parseCookie } from 'cookie';
|
|
3
|
-
import { PikkuRequest } from '../pikku-request.js';
|
|
4
|
-
/**
|
|
5
|
-
* Abstract class representing a pikku request.
|
|
6
|
-
* @template In - The type of the request body.
|
|
7
|
-
* @group RequestResponse
|
|
8
|
-
*/
|
|
9
|
-
export class PikkuHTTPRequest extends PikkuRequest {
|
|
10
|
-
request;
|
|
11
|
-
_params = {};
|
|
12
|
-
url;
|
|
13
|
-
constructor(request) {
|
|
14
|
-
super();
|
|
15
|
-
this.request = request;
|
|
16
|
-
this.url = new URL(request.url);
|
|
17
|
-
}
|
|
18
|
-
method() {
|
|
19
|
-
return this.request.method.toLowerCase();
|
|
20
|
-
}
|
|
21
|
-
path() {
|
|
22
|
-
return this.url.pathname;
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Retrieves the request body.
|
|
26
|
-
* @returns A promise that resolves to the request body.
|
|
27
|
-
*/
|
|
28
|
-
json() {
|
|
29
|
-
return this.request.json();
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Retrieves the raw request body as a Buffer.
|
|
33
|
-
* @returns A promise that resolves to the raw request body.
|
|
34
|
-
*/
|
|
35
|
-
arrayBuffer() {
|
|
36
|
-
return this.request.arrayBuffer();
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Retrieves the value of a specific header.
|
|
40
|
-
* @param headerName - The name of the header to retrieve.
|
|
41
|
-
* @returns The value of the header, or undefined if the header is not found.
|
|
42
|
-
*/
|
|
43
|
-
header(headerName) {
|
|
44
|
-
return this.request.headers.get(headerName.toLowerCase());
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Retrieves the cookies from the request.
|
|
48
|
-
* @returns An object containing the cookies.
|
|
49
|
-
*/
|
|
50
|
-
cookies() {
|
|
51
|
-
const cookieHeader = this.header('cookie');
|
|
52
|
-
if (cookieHeader) {
|
|
53
|
-
return parseCookie(cookieHeader);
|
|
54
|
-
}
|
|
55
|
-
return {};
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Retrieves the request parameters.
|
|
59
|
-
* @returns An object containing the request parameters.
|
|
60
|
-
*/
|
|
61
|
-
params() {
|
|
62
|
-
return this._params;
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Sets the request parameters.
|
|
66
|
-
* @param params - An object containing the request parameters to set.
|
|
67
|
-
*/
|
|
68
|
-
setParams(params) {
|
|
69
|
-
this._params = params;
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Retrieves the query parameters from the request.
|
|
73
|
-
* @returns An object containing the query parameters.
|
|
74
|
-
*/
|
|
75
|
-
query() {
|
|
76
|
-
return parseQuery(this.url.searchParams.toString());
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Retrieves the combined data from the request, including parameters, query, and body.
|
|
80
|
-
* @returns A promise that resolves to an object containing the combined data.
|
|
81
|
-
*/
|
|
82
|
-
async getData() {
|
|
83
|
-
let body = {};
|
|
84
|
-
try {
|
|
85
|
-
body = await this.json();
|
|
86
|
-
}
|
|
87
|
-
catch (e) { }
|
|
88
|
-
return {
|
|
89
|
-
...this.params(),
|
|
90
|
-
...this.query(),
|
|
91
|
-
// TODO: If body isn't an object, we should insert it as the word...
|
|
92
|
-
...body,
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export declare class PikkuHTTPResponse {
|
|
2
|
-
#private;
|
|
3
|
-
status(code: number): this;
|
|
4
|
-
cookie(name: string, value: string, flags: any): this;
|
|
5
|
-
header(name: string, value: string | string[]): this;
|
|
6
|
-
arrayBuffer(data: XMLHttpRequestBodyInit): this;
|
|
7
|
-
json(data: unknown): this;
|
|
8
|
-
text(content: string): this;
|
|
9
|
-
html(content: string): this;
|
|
10
|
-
body(body: BodyInit): this;
|
|
11
|
-
redirect(location: string, status?: number): this;
|
|
12
|
-
toResponse(args?: Record<string, any>): Response;
|
|
13
|
-
}
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
export class PikkuHTTPResponse {
|
|
2
|
-
#statusCode = 200;
|
|
3
|
-
#headers = new Headers();
|
|
4
|
-
#body = null;
|
|
5
|
-
status(code) {
|
|
6
|
-
this.#statusCode = code;
|
|
7
|
-
return this;
|
|
8
|
-
}
|
|
9
|
-
cookie(name, value, flags) {
|
|
10
|
-
// TODO
|
|
11
|
-
return this;
|
|
12
|
-
}
|
|
13
|
-
header(name, value) {
|
|
14
|
-
this.#headers.delete(name);
|
|
15
|
-
if (Array.isArray(value)) {
|
|
16
|
-
value.forEach((v) => this.#headers.append(name, v));
|
|
17
|
-
}
|
|
18
|
-
else {
|
|
19
|
-
this.#headers.set(name, value);
|
|
20
|
-
}
|
|
21
|
-
return this;
|
|
22
|
-
}
|
|
23
|
-
arrayBuffer(data) {
|
|
24
|
-
this.#body = data;
|
|
25
|
-
this.header('Content-Type', 'application/octet-stream');
|
|
26
|
-
return this;
|
|
27
|
-
}
|
|
28
|
-
json(data) {
|
|
29
|
-
this.#body = JSON.stringify(data);
|
|
30
|
-
this.header('Content-Type', 'application/json');
|
|
31
|
-
return this;
|
|
32
|
-
}
|
|
33
|
-
text(content) {
|
|
34
|
-
this.#body = content;
|
|
35
|
-
this.header('Content-Type', 'text/plain');
|
|
36
|
-
return this;
|
|
37
|
-
}
|
|
38
|
-
html(content) {
|
|
39
|
-
this.#body = content;
|
|
40
|
-
this.header('Content-Type', 'text/html');
|
|
41
|
-
return this;
|
|
42
|
-
}
|
|
43
|
-
body(body) {
|
|
44
|
-
this.#body = body;
|
|
45
|
-
return this;
|
|
46
|
-
}
|
|
47
|
-
redirect(location, status = 302) {
|
|
48
|
-
this.#statusCode = status;
|
|
49
|
-
this.header('Location', location);
|
|
50
|
-
this.html(`Redirecting to <a href="${location}">${location}</a>`);
|
|
51
|
-
return this;
|
|
52
|
-
}
|
|
53
|
-
toResponse(args) {
|
|
54
|
-
return new Response(this.#body, {
|
|
55
|
-
...args,
|
|
56
|
-
status: this.#statusCode,
|
|
57
|
-
headers: this.#headers,
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
type TimeUnit = 'second' | 'minute' | 'hour' | 'day' | 'week' | 'year';
|
|
2
|
-
export interface RelativeTimeInput {
|
|
3
|
-
value: number;
|
|
4
|
-
unit: TimeUnit;
|
|
5
|
-
}
|
|
6
|
-
export declare const getRelativeTimeOffset: ({ value, unit }: RelativeTimeInput) => number;
|
|
7
|
-
/**
|
|
8
|
-
* Returns a Unix timestamp (in seconds) offset from now by the given duration.
|
|
9
|
-
* e.g. value = -1 and unit = 'day' => 1 day ago => now - 86400 seconds
|
|
10
|
-
*/
|
|
11
|
-
export declare const getRelativeTimeOffsetFromNow: (relativeTime: RelativeTimeInput) => Date;
|
|
12
|
-
export {};
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
export const getRelativeTimeOffset = ({ value, unit }) => {
|
|
2
|
-
const multipliers = {
|
|
3
|
-
second: 1,
|
|
4
|
-
minute: 60,
|
|
5
|
-
hour: 60 * 60,
|
|
6
|
-
day: 60 * 60 * 24,
|
|
7
|
-
week: 60 * 60 * 24 * 7,
|
|
8
|
-
year: Math.round(365.25 * 60 * 60 * 24),
|
|
9
|
-
};
|
|
10
|
-
return Math.round(value * multipliers[unit]);
|
|
11
|
-
};
|
|
12
|
-
/**
|
|
13
|
-
* Returns a Unix timestamp (in seconds) offset from now by the given duration.
|
|
14
|
-
* e.g. value = -1 and unit = 'day' => 1 day ago => now - 86400 seconds
|
|
15
|
-
*/
|
|
16
|
-
export const getRelativeTimeOffsetFromNow = (relativeTime) => {
|
|
17
|
-
const now = Math.floor(Date.now() / 1000);
|
|
18
|
-
console.log(getRelativeTimeOffset(relativeTime), relativeTime, now);
|
|
19
|
-
return new Date(now + getRelativeTimeOffset(relativeTime));
|
|
20
|
-
};
|
package/dist/pikku-func.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { CoreServices, CoreSingletonServices, CoreUserSession } from "./types/core.types.js";
|
|
2
|
-
import { CoreAPIFunction, CoreAPIFunctionSessionless, CorePermissionGroup } from "./types/functions.types.js";
|
|
3
|
-
export declare const getFunctionName: (func: CoreAPIFunction<any, any> | CoreAPIFunctionSessionless<any, any>) => string;
|
|
4
|
-
export declare const runPikkuFuncDirectly: <In, Out>(funcName: string, allServices: CoreServices, data: In, session?: CoreUserSession) => Promise<Out>;
|
|
5
|
-
export declare const runPikkuFunc: <In = any, Out = any>(funcName: string, { singletonServices, getAllServices, data, session, permissions, coerceDataFromSchema }: {
|
|
6
|
-
singletonServices: CoreSingletonServices;
|
|
7
|
-
getAllServices: () => Promise<CoreServices> | CoreServices;
|
|
8
|
-
data: In;
|
|
9
|
-
session?: CoreUserSession;
|
|
10
|
-
permissions?: CorePermissionGroup;
|
|
11
|
-
coerceDataFromSchema?: boolean;
|
|
12
|
-
}) => Promise<Out>;
|
package/dist/pikku-func.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { ForbiddenError } from "./errors/errors.js";
|
|
2
|
-
import { verifyPermissions } from "./permissions.js";
|
|
3
|
-
import { pikkuState } from "./pikku-state.js";
|
|
4
|
-
import { coerceTopLevelDataFromSchema, validateSchema } from "./schema.js";
|
|
5
|
-
export const getFunctionName = (func) => {
|
|
6
|
-
const funcName = pikkuState('functions', 'functionToName').get(func);
|
|
7
|
-
if (!funcName) {
|
|
8
|
-
throw new Error(`Function not found: ${funcName}`);
|
|
9
|
-
}
|
|
10
|
-
return funcName;
|
|
11
|
-
};
|
|
12
|
-
export const runPikkuFuncDirectly = async (funcName, allServices, data, session) => {
|
|
13
|
-
const func = pikkuState('functions', 'nameToFunction').get(funcName);
|
|
14
|
-
if (!func) {
|
|
15
|
-
throw new Error(`Function not found: ${funcName}`);
|
|
16
|
-
}
|
|
17
|
-
return await func(allServices, data, session);
|
|
18
|
-
};
|
|
19
|
-
export const runPikkuFunc = async (funcName, { singletonServices, getAllServices, data, session, permissions, coerceDataFromSchema }) => {
|
|
20
|
-
const func = pikkuState('functions', 'nameToFunction').get(funcName);
|
|
21
|
-
if (!func) {
|
|
22
|
-
throw new Error(`Function not found: ${funcName}`);
|
|
23
|
-
}
|
|
24
|
-
const funcMeta = pikkuState('functions', 'meta')[funcName];
|
|
25
|
-
const schemaName = funcMeta.schemaName;
|
|
26
|
-
// Validate request data against the defined schema, if any
|
|
27
|
-
await validateSchema(singletonServices.logger, singletonServices.schema, schemaName, data);
|
|
28
|
-
// Coerce (top level) query string parameters or date objects if specified by the schema
|
|
29
|
-
if (coerceDataFromSchema && schemaName) {
|
|
30
|
-
coerceTopLevelDataFromSchema(schemaName, data);
|
|
31
|
-
}
|
|
32
|
-
const allServices = await getAllServices();
|
|
33
|
-
// Execute permission checks
|
|
34
|
-
const permissioned = await verifyPermissions(permissions, allServices, data, session);
|
|
35
|
-
if (permissioned === false) {
|
|
36
|
-
throw new ForbiddenError('Permission denied');
|
|
37
|
-
}
|
|
38
|
-
return await func(allServices, data, session);
|
|
39
|
-
};
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { PikkuChannel } from '../channel/channel.types.js';
|
|
2
|
-
import type { CoreServices, CoreSingletonServices, CoreUserSession } from './core.types.js';
|
|
3
|
-
/**
|
|
4
|
-
* Represents a core API function that performs an operation using core services and a user session.
|
|
5
|
-
*
|
|
6
|
-
* @template In - The input type.
|
|
7
|
-
* @template Out - The output type.
|
|
8
|
-
* @template Services - The services type, defaults to `CoreServices`.
|
|
9
|
-
* @template Session - The session type, defaults to `CoreUserSession`.
|
|
10
|
-
*/
|
|
11
|
-
export type CoreAPIFunction<In, Out, Channel extends boolean = false, Services extends CoreSingletonServices = CoreServices & (Channel extends true ? {
|
|
12
|
-
channel: PikkuChannel<unknown, Out>;
|
|
13
|
-
} : {
|
|
14
|
-
channel?: PikkuChannel<unknown, Out> | undefined;
|
|
15
|
-
}), Session extends CoreUserSession = CoreUserSession> = (services: Services, data: In, session: Session) => Channel extends true ? Promise<Out> | Promise<void> : Promise<Out>;
|
|
16
|
-
/**
|
|
17
|
-
* Represents a core API function that can be used without a session.
|
|
18
|
-
*
|
|
19
|
-
* @template In - The input type.
|
|
20
|
-
* @template Out - The output type.
|
|
21
|
-
* @template Services - The services type, defaults to `CoreServices`.
|
|
22
|
-
* @template Session - The session type, defaults to `CoreUserSession`.
|
|
23
|
-
*/
|
|
24
|
-
export type CoreAPIFunctionSessionless<In, Out, Channel extends boolean = false, Services extends CoreSingletonServices = CoreServices & (Channel extends true ? {
|
|
25
|
-
channel: PikkuChannel<unknown, Out>;
|
|
26
|
-
} : {
|
|
27
|
-
channel?: PikkuChannel<unknown, Out> | undefined;
|
|
28
|
-
}), Session extends CoreUserSession = CoreUserSession> = (services: Services, data: In, session?: Session) => Channel extends true ? Promise<Out> | Promise<void> : Promise<Out>;
|
|
29
|
-
/**
|
|
30
|
-
* Represents a function that checks permissions for a given API operation.
|
|
31
|
-
*
|
|
32
|
-
* @template In - The input type.
|
|
33
|
-
* @template Services - The services type, defaults to `CoreServices`.
|
|
34
|
-
* @template Session - The session type, defaults to `CoreUserSession`.
|
|
35
|
-
*/
|
|
36
|
-
export type CoreAPIPermission<In = any, Services extends CoreSingletonServices = CoreServices, Session extends CoreUserSession = CoreUserSession> = (services: Services, data: In, session?: Session) => Promise<boolean>;
|
|
37
|
-
export type CorePermissionGroup<APIPermission = CoreAPIPermission<any>> = Record<string, APIPermission | APIPermission[]> | undefined;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|