@pikku/core 0.6.26 → 0.7.0
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 +17 -1
- package/dist/channel/channel-handler.js +16 -38
- package/dist/channel/channel-runner.d.ts +7 -8
- package/dist/channel/channel-runner.js +58 -7
- package/dist/channel/channel.types.d.ts +14 -27
- package/dist/channel/local/local-channel-runner.js +9 -4
- package/dist/channel/log-channels.js +4 -4
- package/dist/channel/serverless/serverless-channel-runner.js +17 -6
- package/dist/function/function-runner.d.ts +12 -0
- package/dist/function/function-runner.js +38 -0
- package/dist/function/functions.types.d.ts +37 -0
- package/dist/function/functions.types.js +1 -0
- package/dist/function/index.d.ts +2 -0
- package/dist/function/index.js +2 -0
- package/dist/handle-error.d.ts +1 -1
- package/dist/http/http-route-runner.d.ts +3 -3
- package/dist/http/http-route-runner.js +60 -35
- package/dist/http/http-routes.types.d.ts +38 -7
- package/dist/http/http-runner.d.ts +90 -0
- package/dist/http/http-runner.js +336 -0
- package/dist/http/http.types.d.ts +177 -0
- package/dist/http/http.types.js +1 -0
- package/dist/http/index.d.ts +2 -2
- package/dist/http/index.js +1 -1
- package/dist/http/log-http-routes.js +7 -5
- package/dist/http/pikku-fetch-http-request.d.ts +1 -1
- package/dist/http/pikku-fetch-http-response.d.ts +4 -2
- package/dist/http/pikku-fetch-http-response.js +64 -12
- package/dist/http/pikku-http-request.d.ts +56 -0
- package/dist/http/pikku-http-request.js +95 -0
- package/dist/http/pikku-http-response.d.ts +13 -0
- package/dist/http/pikku-http-response.js +60 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -2
- package/dist/middleware/auth-apikey.js +5 -4
- package/dist/middleware/auth-bearer.js +5 -4
- package/dist/middleware/auth-cookie.js +10 -9
- package/dist/parse-relative-time-offset.d.ts +12 -0
- package/dist/parse-relative-time-offset.js +20 -0
- package/dist/permissions.d.ts +2 -2
- package/dist/pikku-func.d.ts +12 -0
- package/dist/pikku-func.js +39 -0
- package/dist/pikku-function.d.ts +1 -0
- package/dist/pikku-function.js +1 -0
- package/dist/pikku-state.d.ts +10 -5
- package/dist/pikku-state.js +7 -3
- package/dist/scheduler/scheduler-runner.d.ts +1 -1
- package/dist/scheduler/scheduler-runner.js +24 -6
- package/dist/scheduler/scheduler.types.d.ts +3 -2
- package/dist/services/content-service.d.ts +34 -36
- package/dist/services/index.d.ts +1 -0
- package/dist/services/index.js +1 -0
- package/dist/services/local-content.d.ts +4 -2
- package/dist/services/local-content.js +19 -9
- package/dist/types/core.types.d.ts +15 -1
- package/dist/types/functions.types.d.ts +11 -6
- package/dist/utils.d.ts +2 -0
- package/dist/utils.js +15 -0
- package/package.json +2 -1
- package/lcov.info +0 -4310
- package/src/channel/channel-handler.ts +0 -227
- package/src/channel/channel-runner.ts +0 -98
- package/src/channel/channel-store.ts +0 -29
- package/src/channel/channel.types.ts +0 -171
- package/src/channel/eventhub-service.ts +0 -30
- package/src/channel/eventhub-store.ts +0 -8
- package/src/channel/index.ts +0 -8
- package/src/channel/local/index.ts +0 -3
- package/src/channel/local/local-channel-handler.ts +0 -50
- package/src/channel/local/local-channel-runner.test.ts +0 -158
- package/src/channel/local/local-channel-runner.ts +0 -126
- package/src/channel/local/local-eventhub-service.test.ts +0 -95
- package/src/channel/local/local-eventhub-service.ts +0 -95
- package/src/channel/log-channels.ts +0 -20
- package/src/channel/pikku-abstract-channel-handler.test.ts +0 -52
- package/src/channel/pikku-abstract-channel-handler.ts +0 -38
- package/src/channel/serverless/index.ts +0 -1
- package/src/channel/serverless/serverless-channel-runner.ts +0 -209
- package/src/errors/error-handler.ts +0 -61
- package/src/errors/error.test.ts +0 -54
- package/src/errors/errors.ts +0 -339
- package/src/errors/index.ts +0 -2
- package/src/handle-error.ts +0 -73
- package/src/http/http-route-runner.test.ts +0 -117
- package/src/http/http-route-runner.ts +0 -460
- package/src/http/http-routes.types.ts +0 -211
- package/src/http/incomingmessage-to-request-convertor.ts +0 -0
- package/src/http/index.ts +0 -8
- package/src/http/log-http-routes.ts +0 -20
- package/src/http/pikku-fetch-http-request.test.ts +0 -237
- package/src/http/pikku-fetch-http-request.ts +0 -167
- package/src/http/pikku-fetch-http-response.test.ts +0 -82
- package/src/http/pikku-fetch-http-response.ts +0 -84
- package/src/index.ts +0 -19
- package/src/middleware/auth-apikey.ts +0 -65
- package/src/middleware/auth-bearer.ts +0 -65
- package/src/middleware/auth-cookie.ts +0 -98
- package/src/middleware/index.ts +0 -3
- package/src/middleware/timeout.ts +0 -13
- package/src/middleware-runner.ts +0 -43
- package/src/permissions.test.ts +0 -58
- package/src/permissions.ts +0 -45
- package/src/pikku-request.ts +0 -23
- package/src/pikku-response.ts +0 -5
- package/src/pikku-state.ts +0 -71
- package/src/scheduler/index.ts +0 -5
- package/src/scheduler/log-schedulers.ts +0 -20
- package/src/scheduler/scheduler-runner.ts +0 -87
- package/src/scheduler/scheduler.types.ts +0 -29
- package/src/schema.test.ts +0 -57
- package/src/schema.ts +0 -99
- package/src/services/content-service.ts +0 -69
- package/src/services/index.ts +0 -17
- package/src/services/jwt-service.ts +0 -30
- package/src/services/local-content.ts +0 -91
- package/src/services/local-secrets.ts +0 -43
- package/src/services/local-variables.ts +0 -17
- package/src/services/logger-console.ts +0 -97
- package/src/services/logger.ts +0 -57
- package/src/services/schema-service.ts +0 -26
- package/src/services/secret-service.ts +0 -17
- package/src/services/user-session-service.ts +0 -53
- package/src/services/variables-service.ts +0 -6
- package/src/time-utils.test.ts +0 -56
- package/src/time-utils.ts +0 -32
- package/src/types/core.types.ts +0 -159
- package/src/types/functions.types.ts +0 -63
- package/src/utils.ts +0 -21
- package/tsconfig.json +0 -13
- package/tsconfig.tsbuildinfo +0 -1
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
import { verifyPermissions } from '../permissions.js';
|
|
2
1
|
import { match } from 'path-to-regexp';
|
|
3
|
-
import {
|
|
4
|
-
import { closeSessionServices } from '../utils.js';
|
|
5
|
-
import { coerceTopLevelDataFromSchema, validateSchema } from '../schema.js';
|
|
2
|
+
import { MissingSessionError, NotFoundError, } from '../errors/errors.js';
|
|
3
|
+
import { closeSessionServices, createWeakUID, isSerializable, } from '../utils.js';
|
|
6
4
|
import { PikkuUserSessionService, } from '../services/user-session-service.js';
|
|
7
5
|
import { runMiddleware } from '../middleware-runner.js';
|
|
8
6
|
import { handleError } from '../handle-error.js';
|
|
9
7
|
import { pikkuState } from '../pikku-state.js';
|
|
10
8
|
import { PikkuFetchHTTPResponse } from './pikku-fetch-http-response.js';
|
|
11
9
|
import { PikkuFetchHTTPRequest } from './pikku-fetch-http-request.js';
|
|
10
|
+
import { getFunctionName, runPikkuFunc } from '../function/function-runner.js';
|
|
12
11
|
/**
|
|
13
12
|
* Registers middleware either globally or for a specific route.
|
|
14
13
|
*
|
|
@@ -45,7 +44,7 @@ export const addMiddleware = (routeOrMiddleware, middleware) => {
|
|
|
45
44
|
* @template Route Route pattern as a string.
|
|
46
45
|
* @template APIFunction Type for the route handler function.
|
|
47
46
|
* @template APIFunctionSessionless Type for a sessionless handler.
|
|
48
|
-
* @template
|
|
47
|
+
* @template APIPermissionGroup Type representing required permissions.
|
|
49
48
|
* @template APIMiddleware Middleware type to be used with the route.
|
|
50
49
|
* @param {CoreHTTPFunctionRoute<In, Out, Route, APIFunction, APIFunctionSessionless, APIPermission, APIMiddleware>} route - The route configuration object.
|
|
51
50
|
*/
|
|
@@ -66,7 +65,6 @@ export const addRoute = (route) => {
|
|
|
66
65
|
const getMatchingRoute = (requestType, requestPath) => {
|
|
67
66
|
const routes = pikkuState('http', 'routes');
|
|
68
67
|
const middleware = pikkuState('http', 'middleware');
|
|
69
|
-
const routesMeta = pikkuState('http', 'meta');
|
|
70
68
|
for (const route of routes) {
|
|
71
69
|
// Skip routes that don't match the HTTP method
|
|
72
70
|
if (route.method !== requestType.toLowerCase()) {
|
|
@@ -84,14 +82,12 @@ const getMatchingRoute = (requestType, requestPath) => {
|
|
|
84
82
|
.filter((m) => m.route === '*' || new RegExp(m.route).test(route.route))
|
|
85
83
|
.map((m) => m.middleware)
|
|
86
84
|
.flat();
|
|
87
|
-
// Extract associated schema information if available
|
|
88
|
-
const schemaName = routesMeta.find((routeMeta) => routeMeta.method === route.method && routeMeta.route === route.route)?.input;
|
|
89
85
|
return {
|
|
90
86
|
matchedPath,
|
|
91
87
|
params: matchedPath.params,
|
|
92
88
|
route,
|
|
89
|
+
permissions: route.permissions,
|
|
93
90
|
middleware: [...globalMiddleware, ...(route.middleware || [])],
|
|
94
|
-
schemaName,
|
|
95
91
|
};
|
|
96
92
|
}
|
|
97
93
|
}
|
|
@@ -141,8 +137,8 @@ export const createHTTPInteraction = (request, response) => {
|
|
|
141
137
|
* @throws Throws errors like MissingSessionError or ForbiddenError on validation failures.
|
|
142
138
|
*/
|
|
143
139
|
const executeRouteWithMiddleware = async (services, matchedRoute, http, options) => {
|
|
144
|
-
const { matchedPath, params, route, middleware
|
|
145
|
-
const { singletonServices, userSession, createSessionServices, skipUserSession, } = services;
|
|
140
|
+
const { matchedPath, params, route, middleware } = matchedRoute;
|
|
141
|
+
const { singletonServices, userSession, createSessionServices, skipUserSession, requestId, } = services;
|
|
146
142
|
const requiresSession = route.auth !== false;
|
|
147
143
|
let sessionServices;
|
|
148
144
|
let result;
|
|
@@ -163,28 +159,54 @@ const executeRouteWithMiddleware = async (services, matchedRoute, http, options)
|
|
|
163
159
|
});
|
|
164
160
|
throw new MissingSessionError();
|
|
165
161
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
162
|
+
const data = http.request.data();
|
|
163
|
+
const getAllServices = async () => {
|
|
164
|
+
let channel;
|
|
165
|
+
if (matchedRoute.route.sse) {
|
|
166
|
+
const response = http?.response;
|
|
167
|
+
if (!response) {
|
|
168
|
+
throw new Error('SSE requires a valid HTTP response object');
|
|
169
|
+
}
|
|
170
|
+
if (!response.setMode) {
|
|
171
|
+
throw new Error('Response object does not support SSE mode');
|
|
172
|
+
}
|
|
173
|
+
response.setMode('stream');
|
|
174
|
+
response.header('Content-Type', 'text/event-stream');
|
|
175
|
+
response.header('Cache-Control', 'no-cache');
|
|
176
|
+
response.header('Connection', 'keep-alive');
|
|
177
|
+
response.header('Transfer-Encoding', 'chunked');
|
|
178
|
+
channel = {
|
|
179
|
+
channelId: requestId,
|
|
180
|
+
openingData: data,
|
|
181
|
+
send: (data) => {
|
|
182
|
+
response.arrayBuffer(isSerializable(data) ? JSON.stringify(data) : data);
|
|
183
|
+
},
|
|
184
|
+
close: () => {
|
|
185
|
+
channel.state = 'closed';
|
|
186
|
+
response.close?.();
|
|
187
|
+
},
|
|
188
|
+
state: 'open',
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
// Create session-specific services for handling the request
|
|
192
|
+
sessionServices = await createSessionServices({ ...singletonServices, userSession, channel }, { http }, session);
|
|
193
|
+
return {
|
|
194
|
+
...singletonServices,
|
|
195
|
+
...sessionServices,
|
|
196
|
+
http,
|
|
197
|
+
userSession,
|
|
198
|
+
channel,
|
|
199
|
+
};
|
|
173
200
|
};
|
|
174
|
-
const
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
if (permissioned === false) {
|
|
184
|
-
throw new ForbiddenError('Permission denied');
|
|
185
|
-
}
|
|
186
|
-
// Invoke the actual route handler function
|
|
187
|
-
result = await route.func(allServices, data, session);
|
|
201
|
+
const funName = getFunctionName(route.func);
|
|
202
|
+
const result = await runPikkuFunc(funName, {
|
|
203
|
+
singletonServices,
|
|
204
|
+
getAllServices,
|
|
205
|
+
session,
|
|
206
|
+
data,
|
|
207
|
+
permissions: route.permissions,
|
|
208
|
+
coerceDataFromSchema: options.coerceDataFromSchema,
|
|
209
|
+
});
|
|
188
210
|
// Respond with either a binary or JSON response based on configuration
|
|
189
211
|
if (route.returnsJSON === false) {
|
|
190
212
|
http?.response?.arrayBuffer(result);
|
|
@@ -256,7 +278,10 @@ export const pikkuFetch = async (request, params) => {
|
|
|
256
278
|
* @param {RunRouteOptions & RunRouteParams} options - Options such as singleton services, session handling, and error configuration.
|
|
257
279
|
* @returns {Promise<Out | void>} The output from the route handler or void if an error occurred.
|
|
258
280
|
*/
|
|
259
|
-
export const fetchData = async (request, response, { singletonServices, createSessionServices, skipUserSession = false, respondWith404 = true, logWarningsForStatusCodes = [], coerceDataFromSchema = true, bubbleErrors = false, }) => {
|
|
281
|
+
export const fetchData = async (request, response, { singletonServices, createSessionServices, skipUserSession = false, respondWith404 = true, logWarningsForStatusCodes = [], coerceDataFromSchema = true, bubbleErrors = false, generateRequestId, }) => {
|
|
282
|
+
const requestId = request.getHeader?.('x-request-id') ||
|
|
283
|
+
generateRequestId?.() ||
|
|
284
|
+
createWeakUID();
|
|
260
285
|
const userSession = new PikkuUserSessionService();
|
|
261
286
|
let sessionServices;
|
|
262
287
|
let result;
|
|
@@ -283,13 +308,13 @@ export const fetchData = async (request, response, { singletonServices, createSe
|
|
|
283
308
|
userSession,
|
|
284
309
|
createSessionServices,
|
|
285
310
|
skipUserSession,
|
|
311
|
+
requestId,
|
|
286
312
|
}, matchedRoute, http, { coerceDataFromSchema }));
|
|
287
313
|
return result;
|
|
288
314
|
}
|
|
289
315
|
catch (e) {
|
|
290
316
|
// Handle errors and, depending on configuration, bubble them up or respond with an error
|
|
291
|
-
handleError(e, http,
|
|
292
|
-
singletonServices.logger, logWarningsForStatusCodes, respondWith404, bubbleErrors);
|
|
317
|
+
handleError(e, http, requestId, singletonServices.logger, logWarningsForStatusCodes, respondWith404, bubbleErrors);
|
|
293
318
|
}
|
|
294
319
|
finally {
|
|
295
320
|
// Clean up any session-specific services created during processing
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { SerializeOptions } from 'cookie';
|
|
2
|
-
import { PikkuError } from '../errors/error-handler.js';
|
|
3
|
-
import { APIDocs, CoreServices, CoreSingletonServices, CoreUserSession, CreateSessionServices, PikkuMiddleware } from '../types/core.types.js';
|
|
4
|
-
import { CoreAPIFunction, CoreAPIFunctionSessionless, CoreAPIPermission } from '../
|
|
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
5
|
type ExtractRouteParams<S extends string> = S extends `${string}:${infer Param}/${infer Rest}` ? Param | ExtractRouteParams<`/${Rest}`> : S extends `${string}:${infer Param}` ? Param : never;
|
|
6
6
|
export type AssertRouteParams<In, Route extends string> = ExtractRouteParams<Route> extends keyof In ? unknown : ['Error: Route parameters', ExtractRouteParams<Route>, 'not in', keyof In];
|
|
7
7
|
export type RunRouteOptions = Partial<{
|
|
@@ -10,6 +10,7 @@ export type RunRouteOptions = Partial<{
|
|
|
10
10
|
logWarningsForStatusCodes: number[];
|
|
11
11
|
coerceDataFromSchema: boolean;
|
|
12
12
|
bubbleErrors: boolean;
|
|
13
|
+
generateRequestId: () => string;
|
|
13
14
|
}>;
|
|
14
15
|
export type RunRouteParams = {
|
|
15
16
|
singletonServices: CoreSingletonServices;
|
|
@@ -64,10 +65,11 @@ export type CoreHTTPFunctionRoute<In, Out, R extends string, APIFunction = CoreA
|
|
|
64
65
|
route: R;
|
|
65
66
|
method: HTTPMethod;
|
|
66
67
|
func: APIFunction;
|
|
67
|
-
permissions?:
|
|
68
|
+
permissions?: CorePermissionGroup<APIPermission>;
|
|
68
69
|
auth?: true;
|
|
69
70
|
tags?: string[];
|
|
70
71
|
middleware?: APIMiddleware[];
|
|
72
|
+
sse?: undefined;
|
|
71
73
|
}) | (CoreHTTPFunction & {
|
|
72
74
|
route: R;
|
|
73
75
|
method: HTTPMethod;
|
|
@@ -76,15 +78,35 @@ export type CoreHTTPFunctionRoute<In, Out, R extends string, APIFunction = CoreA
|
|
|
76
78
|
auth?: false;
|
|
77
79
|
tags?: string[];
|
|
78
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[];
|
|
79
100
|
}) | (CoreHTTPFunction & {
|
|
80
101
|
route: R;
|
|
81
102
|
method: 'post';
|
|
82
103
|
func: APIFunction;
|
|
83
|
-
permissions?:
|
|
104
|
+
permissions?: CorePermissionGroup<APIPermission>;
|
|
84
105
|
auth?: true;
|
|
85
106
|
query?: Array<keyof In>;
|
|
86
107
|
tags?: string[];
|
|
87
108
|
middleware?: APIMiddleware[];
|
|
109
|
+
sse?: undefined;
|
|
88
110
|
}) | (CoreHTTPFunction & {
|
|
89
111
|
route: R;
|
|
90
112
|
method: 'post';
|
|
@@ -94,6 +116,7 @@ export type CoreHTTPFunctionRoute<In, Out, R extends string, APIFunction = CoreA
|
|
|
94
116
|
query?: Array<keyof In>;
|
|
95
117
|
tags?: string[];
|
|
96
118
|
middleware?: APIMiddleware[];
|
|
119
|
+
sse?: undefined;
|
|
97
120
|
});
|
|
98
121
|
/**
|
|
99
122
|
* Represents an array of core API routes.
|
|
@@ -120,6 +143,12 @@ export type HTTPRoutesMeta = Array<{
|
|
|
120
143
|
inputTypes?: HTTPFunctionMetaInputTypes;
|
|
121
144
|
docs?: APIDocs;
|
|
122
145
|
tags?: string[];
|
|
146
|
+
sse?: true;
|
|
147
|
+
}>;
|
|
148
|
+
export type HTTPFunctionsMeta = Array<{
|
|
149
|
+
name: string;
|
|
150
|
+
inputs: string[] | null;
|
|
151
|
+
outputs: string[] | null;
|
|
123
152
|
}>;
|
|
124
153
|
export type HTTPRouteMiddleware = {
|
|
125
154
|
route: string;
|
|
@@ -141,8 +170,10 @@ export interface PikkuHTTPResponse {
|
|
|
141
170
|
status(code: number): this;
|
|
142
171
|
cookie(name: string, value: string | null, options: SerializeOptions): this;
|
|
143
172
|
header(name: string, value: string | string[]): this;
|
|
144
|
-
arrayBuffer(data:
|
|
173
|
+
arrayBuffer(data: ArrayBuffer | ArrayBufferView | Blob | string | FormData | URLSearchParams | ReadableStream): this;
|
|
145
174
|
json(data: unknown): this;
|
|
146
175
|
redirect(location: string, status?: number): this;
|
|
176
|
+
close?: () => void;
|
|
177
|
+
setMode?: (mode: 'stream') => void;
|
|
147
178
|
}
|
|
148
179
|
export {};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { CoreHTTPFunctionRoute, RunRouteOptions, RunRouteParams, PikkuHTTP, PikkuHTTPRequest, PikkuHTTPResponse } from './http.types.js';
|
|
2
|
+
import { PikkuMiddleware } from '../types/core.types.js';
|
|
3
|
+
import { PikkuFetchHTTPResponse } from './pikku-fetch-http-response.js';
|
|
4
|
+
/**
|
|
5
|
+
* Registers middleware either globally or for a specific route.
|
|
6
|
+
*
|
|
7
|
+
* When a string route pattern is provided along with middleware, the middleware
|
|
8
|
+
* is applied only to that route. Otherwise, if an array is provided, it is treated
|
|
9
|
+
* as global middleware (applied to all routes).
|
|
10
|
+
*
|
|
11
|
+
* @template APIMiddleware The middleware type.
|
|
12
|
+
* @param {APIMiddleware[] | string} routeOrMiddleware - Either a global middleware array or a route pattern string.
|
|
13
|
+
* @param {APIMiddleware[]} [middleware] - The middleware array to apply when a route pattern is specified.
|
|
14
|
+
*/
|
|
15
|
+
export declare const addMiddleware: <APIMiddleware extends PikkuMiddleware>(routeOrMiddleware: APIMiddleware[] | string, middleware?: APIMiddleware[]) => void;
|
|
16
|
+
/**
|
|
17
|
+
* Adds a new route to the global HTTP route registry.
|
|
18
|
+
*
|
|
19
|
+
* The route configuration includes the HTTP method, route path, permissions,
|
|
20
|
+
* middleware, and the handler function that implements the route's logic.
|
|
21
|
+
*
|
|
22
|
+
* @template In Expected input type.
|
|
23
|
+
* @template Out Expected output type.
|
|
24
|
+
* @template Route Route pattern as a string.
|
|
25
|
+
* @template APIFunction Type for the route handler function.
|
|
26
|
+
* @template APIFunctionSessionless Type for a sessionless handler.
|
|
27
|
+
* @template APIPermissionGroup Type representing required permissions.
|
|
28
|
+
* @template APIMiddleware Middleware type to be used with the route.
|
|
29
|
+
* @param {CoreHTTPFunctionRoute<In, Out, Route, APIFunction, APIFunctionSessionless, APIPermission, APIMiddleware>} route - The route configuration object.
|
|
30
|
+
*/
|
|
31
|
+
export declare const addHTTPRoute: <In, Out, Route extends string, APIFunction, APIFunctionSessionless, APIPermissionGroup, APIMiddleware>(httpRoute: CoreHTTPFunctionRoute<In, Out, Route, APIFunction, APIFunctionSessionless, APIPermissionGroup, APIMiddleware>) => void;
|
|
32
|
+
/**
|
|
33
|
+
* Combines the request and response objects into a single HTTP interaction object.
|
|
34
|
+
*
|
|
35
|
+
* This utility function creates an object that holds both the HTTP request and response,
|
|
36
|
+
* which simplifies passing these around through middleware and route execution.
|
|
37
|
+
*
|
|
38
|
+
* @param {PikkuHTTPRequest | undefined} request - The HTTP request object.
|
|
39
|
+
* @param {PikkuHTTPResponse | undefined} response - The HTTP response object.
|
|
40
|
+
* @returns {PikkuHTTP | undefined} The combined HTTP interaction object or undefined if none provided.
|
|
41
|
+
*/
|
|
42
|
+
export declare const createHTTPInteraction: (request: PikkuHTTPRequest | undefined, response: PikkuHTTPResponse | undefined) => PikkuHTTP | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Executes an HTTP route for a given Fetch API request.
|
|
45
|
+
*
|
|
46
|
+
* This function wraps the entire lifecycle of handling an HTTP request:
|
|
47
|
+
* - Matching the request to a registered route.
|
|
48
|
+
* - Validating input and session state.
|
|
49
|
+
* - Running middleware and the route handler.
|
|
50
|
+
* - Handling errors and forming the response.
|
|
51
|
+
*
|
|
52
|
+
* @template In Expected input data type.
|
|
53
|
+
* @template Out Expected output data type.
|
|
54
|
+
* @param {Request} request - The native Fetch API Request object.
|
|
55
|
+
* @param {RunRouteOptions & RunRouteParams} params - Additional options including services and session management.
|
|
56
|
+
* @returns {Promise<Response>} A promise that resolves to a Fetch API Response object.
|
|
57
|
+
*/
|
|
58
|
+
export declare const fetch: <In, Out>(request: Request, params: RunRouteOptions & RunRouteParams) => Promise<Response>;
|
|
59
|
+
/**
|
|
60
|
+
* Executes an HTTP route using a Pikku-specific request wrapper.
|
|
61
|
+
*
|
|
62
|
+
* This variant accepts either a native Request or a PikkuHTTPRequest object and returns
|
|
63
|
+
* a PikkuFetchHTTPResponse for further manipulation if needed.
|
|
64
|
+
*
|
|
65
|
+
* @template In Expected input data type.
|
|
66
|
+
* @template Out Expected output data type.
|
|
67
|
+
* @param {Request | PikkuHTTPRequest} request - The request object.
|
|
68
|
+
* @param {RunRouteOptions & RunRouteParams} params - Execution options including services and session configuration.
|
|
69
|
+
* @returns {Promise<PikkuFetchHTTPResponse>} A promise that resolves to a PikkuFetchHTTPResponse object.
|
|
70
|
+
*/
|
|
71
|
+
export declare const pikkuFetch: <In, Out>(request: Request | PikkuHTTPRequest, params: RunRouteOptions & RunRouteParams) => Promise<PikkuFetchHTTPResponse>;
|
|
72
|
+
/**
|
|
73
|
+
* Core function to process an HTTP request through route matching, validation,
|
|
74
|
+
* middleware execution, error handling, and session service cleanup.
|
|
75
|
+
*
|
|
76
|
+
* This function does the following:
|
|
77
|
+
* - Wraps the incoming request and response into an HTTP interaction object.
|
|
78
|
+
* - Determines the correct route based on HTTP method and path.
|
|
79
|
+
* - Executes middleware and the route handler.
|
|
80
|
+
* - Catches and handles errors, optionally bubbling them if configured.
|
|
81
|
+
* - Cleans up any session services created during processing.
|
|
82
|
+
*
|
|
83
|
+
* @template In Expected input data type.
|
|
84
|
+
* @template Out Expected output data type.
|
|
85
|
+
* @param {Request | PikkuHTTPRequest} request - The incoming HTTP request.
|
|
86
|
+
* @param {PikkuHTTPResponse} response - The response object to be populated.
|
|
87
|
+
* @param {RunRouteOptions & RunRouteParams} options - Options such as singleton services, session handling, and error configuration.
|
|
88
|
+
* @returns {Promise<Out | void>} The output from the route handler or void if an error occurred.
|
|
89
|
+
*/
|
|
90
|
+
export declare const fetchData: <In, Out>(request: Request | PikkuHTTPRequest, response: PikkuHTTPResponse, { singletonServices, createSessionServices, skipUserSession, respondWith404, logWarningsForStatusCodes, coerceDataFromSchema, bubbleErrors, generateRequestId, }: RunRouteOptions & RunRouteParams) => Promise<Out | void>;
|