@hyperspan/framework 1.0.10 → 1.0.12
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/package.json +1 -1
- package/src/actions.ts +4 -4
- package/src/server.ts +36 -19
- package/src/types.ts +11 -6
package/package.json
CHANGED
package/src/actions.ts
CHANGED
|
@@ -109,12 +109,12 @@ export function createAction<T extends z.ZodObject<any, any>>(params: { name: st
|
|
|
109
109
|
_errorHandler = handler;
|
|
110
110
|
return api;
|
|
111
111
|
},
|
|
112
|
-
use(middleware: HS.MiddlewareFunction) {
|
|
113
|
-
route.use(middleware);
|
|
112
|
+
use(middleware: HS.MiddlewareFunction, opts?: HS.MiddlewareMethodOptions) {
|
|
113
|
+
route.use(middleware, opts);
|
|
114
114
|
return api;
|
|
115
115
|
},
|
|
116
|
-
middleware(middleware: Array<HS.MiddlewareFunction
|
|
117
|
-
route.middleware(middleware);
|
|
116
|
+
middleware(middleware: Array<HS.MiddlewareFunction>, opts?: HS.MiddlewareMethodOptions) {
|
|
117
|
+
route.middleware(middleware, opts);
|
|
118
118
|
return api;
|
|
119
119
|
},
|
|
120
120
|
fetch: route.fetch,
|
package/src/server.ts
CHANGED
|
@@ -122,11 +122,11 @@ export function createContext(req: Request, route?: HS.Route): HS.Context {
|
|
|
122
122
|
export function createRoute(config: Partial<HS.RouteConfig> = {}): HS.Route {
|
|
123
123
|
const _handlers: Record<string, HS.RouteHandler> = {};
|
|
124
124
|
let _errorHandler: HS.ErrorHandler | undefined = undefined;
|
|
125
|
-
let _middleware: Record<string, Array<HS.MiddlewareFunction>> = { '*': [] };
|
|
126
125
|
|
|
127
126
|
const api: HS.Route = {
|
|
128
127
|
_kind: 'hsRoute',
|
|
129
128
|
_config: config,
|
|
129
|
+
_middleware: { GET: [], POST: [], PUT: [], PATCH: [], DELETE: [], HEAD: [], OPTIONS: [], '*': [] },
|
|
130
130
|
_methods: () => Object.keys(_handlers),
|
|
131
131
|
_path() {
|
|
132
132
|
if (this._config.path) {
|
|
@@ -141,7 +141,7 @@ export function createRoute(config: Partial<HS.RouteConfig> = {}): HS.Route {
|
|
|
141
141
|
*/
|
|
142
142
|
get(handler: HS.RouteHandler, handlerOptions?: HS.RouteHandlerOptions) {
|
|
143
143
|
_handlers['GET'] = handler;
|
|
144
|
-
_middleware['GET'] = handlerOptions?.middleware || [];
|
|
144
|
+
api._middleware['GET'] = handlerOptions?.middleware || [];
|
|
145
145
|
return api;
|
|
146
146
|
},
|
|
147
147
|
/**
|
|
@@ -149,7 +149,7 @@ export function createRoute(config: Partial<HS.RouteConfig> = {}): HS.Route {
|
|
|
149
149
|
*/
|
|
150
150
|
post(handler: HS.RouteHandler, handlerOptions?: HS.RouteHandlerOptions) {
|
|
151
151
|
_handlers['POST'] = handler;
|
|
152
|
-
_middleware['POST'] = handlerOptions?.middleware || [];
|
|
152
|
+
api._middleware['POST'] = handlerOptions?.middleware || [];
|
|
153
153
|
return api;
|
|
154
154
|
},
|
|
155
155
|
/**
|
|
@@ -157,7 +157,7 @@ export function createRoute(config: Partial<HS.RouteConfig> = {}): HS.Route {
|
|
|
157
157
|
*/
|
|
158
158
|
put(handler: HS.RouteHandler, handlerOptions?: HS.RouteHandlerOptions) {
|
|
159
159
|
_handlers['PUT'] = handler;
|
|
160
|
-
_middleware['PUT'] = handlerOptions?.middleware || [];
|
|
160
|
+
api._middleware['PUT'] = handlerOptions?.middleware || [];
|
|
161
161
|
return api;
|
|
162
162
|
},
|
|
163
163
|
/**
|
|
@@ -165,7 +165,7 @@ export function createRoute(config: Partial<HS.RouteConfig> = {}): HS.Route {
|
|
|
165
165
|
*/
|
|
166
166
|
patch(handler: HS.RouteHandler, handlerOptions?: HS.RouteHandlerOptions) {
|
|
167
167
|
_handlers['PATCH'] = handler;
|
|
168
|
-
_middleware['PATCH'] = handlerOptions?.middleware || [];
|
|
168
|
+
api._middleware['PATCH'] = handlerOptions?.middleware || [];
|
|
169
169
|
return api;
|
|
170
170
|
},
|
|
171
171
|
/**
|
|
@@ -173,7 +173,7 @@ export function createRoute(config: Partial<HS.RouteConfig> = {}): HS.Route {
|
|
|
173
173
|
*/
|
|
174
174
|
delete(handler: HS.RouteHandler, handlerOptions?: HS.RouteHandlerOptions) {
|
|
175
175
|
_handlers['DELETE'] = handler;
|
|
176
|
-
_middleware['DELETE'] = handlerOptions?.middleware || [];
|
|
176
|
+
api._middleware['DELETE'] = handlerOptions?.middleware || [];
|
|
177
177
|
return api;
|
|
178
178
|
},
|
|
179
179
|
/**
|
|
@@ -181,7 +181,7 @@ export function createRoute(config: Partial<HS.RouteConfig> = {}): HS.Route {
|
|
|
181
181
|
*/
|
|
182
182
|
options(handler: HS.RouteHandler, handlerOptions?: HS.RouteHandlerOptions) {
|
|
183
183
|
_handlers['OPTIONS'] = handler;
|
|
184
|
-
_middleware['OPTIONS'] = handlerOptions?.middleware || [];
|
|
184
|
+
api._middleware['OPTIONS'] = handlerOptions?.middleware || [];
|
|
185
185
|
return api;
|
|
186
186
|
},
|
|
187
187
|
/**
|
|
@@ -189,7 +189,7 @@ export function createRoute(config: Partial<HS.RouteConfig> = {}): HS.Route {
|
|
|
189
189
|
*/
|
|
190
190
|
all(handler: HS.RouteHandler, handlerOptions?: HS.RouteHandlerOptions) {
|
|
191
191
|
_handlers['*'] = handler;
|
|
192
|
-
_middleware['*'] = handlerOptions?.middleware || [];
|
|
192
|
+
api._middleware['*'] = handlerOptions?.middleware || [];
|
|
193
193
|
return api;
|
|
194
194
|
},
|
|
195
195
|
/**
|
|
@@ -202,16 +202,28 @@ export function createRoute(config: Partial<HS.RouteConfig> = {}): HS.Route {
|
|
|
202
202
|
/**
|
|
203
203
|
* Add a middleware function to this route (for all HTTP methods) (non-destructive)
|
|
204
204
|
*/
|
|
205
|
-
use(middleware: HS.MiddlewareFunction) {
|
|
206
|
-
|
|
205
|
+
use(middleware: HS.MiddlewareFunction, opts: HS.MiddlewareMethodOptions = {}) {
|
|
206
|
+
if (opts?.methods) {
|
|
207
|
+
opts.methods.forEach(method => {
|
|
208
|
+
api._middleware[method].push(middleware);
|
|
209
|
+
});
|
|
210
|
+
} else {
|
|
211
|
+
api._middleware['*'].push(middleware);
|
|
212
|
+
}
|
|
207
213
|
return api;
|
|
208
214
|
},
|
|
209
215
|
/**
|
|
210
216
|
* Set the complete middleware stack for this route (for all HTTP methods) (destructive)
|
|
211
217
|
* NOTE: This will override the middleware stack for this route
|
|
212
218
|
*/
|
|
213
|
-
middleware(middleware: Array<HS.MiddlewareFunction
|
|
214
|
-
|
|
219
|
+
middleware(middleware: Array<HS.MiddlewareFunction>, opts: HS.MiddlewareMethodOptions = {}) {
|
|
220
|
+
if (opts?.methods) {
|
|
221
|
+
opts.methods.forEach(method => {
|
|
222
|
+
api._middleware[method] = middleware;
|
|
223
|
+
});
|
|
224
|
+
} else {
|
|
225
|
+
api._middleware['*'] = middleware;
|
|
226
|
+
}
|
|
215
227
|
return api;
|
|
216
228
|
},
|
|
217
229
|
|
|
@@ -220,9 +232,9 @@ export function createRoute(config: Partial<HS.RouteConfig> = {}): HS.Route {
|
|
|
220
232
|
*/
|
|
221
233
|
async fetch(request: Request) {
|
|
222
234
|
const context = createContext(request, api);
|
|
223
|
-
const method = context.req.method;
|
|
224
|
-
const globalMiddleware = _middleware['*'] || [];
|
|
225
|
-
const methodMiddleware = _middleware[method] || [];
|
|
235
|
+
const method = context.req.method as HS.MiddlewareMethod;
|
|
236
|
+
const globalMiddleware = api._middleware['*'] || [];
|
|
237
|
+
const methodMiddleware = api._middleware[method] || [];
|
|
226
238
|
|
|
227
239
|
const methodHandler = async (context: HS.Context) => {
|
|
228
240
|
// Handle CORS preflight requests (if no OPTIONS handler is defined)
|
|
@@ -306,7 +318,6 @@ export function createRoute(config: Partial<HS.RouteConfig> = {}): HS.Route {
|
|
|
306
318
|
* Creates a server object that can compose routes and middleware
|
|
307
319
|
*/
|
|
308
320
|
export async function createServer(config: HS.Config = {} as HS.Config): Promise<HS.Server> {
|
|
309
|
-
const _middleware: HS.MiddlewareFunction[] = [];
|
|
310
321
|
const _routes: HS.Route[] = [];
|
|
311
322
|
|
|
312
323
|
// Load plugins, if any
|
|
@@ -317,9 +328,15 @@ export async function createServer(config: HS.Config = {} as HS.Config): Promise
|
|
|
317
328
|
const api: HS.Server = {
|
|
318
329
|
_config: config,
|
|
319
330
|
_routes: _routes,
|
|
320
|
-
_middleware:
|
|
321
|
-
use(middleware: HS.MiddlewareFunction) {
|
|
322
|
-
|
|
331
|
+
_middleware: { GET: [], POST: [], PUT: [], PATCH: [], DELETE: [], HEAD: [], OPTIONS: [], '*': [] },
|
|
332
|
+
use(middleware: HS.MiddlewareFunction, opts?: HS.MiddlewareMethodOptions) {
|
|
333
|
+
if (opts?.methods) {
|
|
334
|
+
opts.methods.forEach(method => {
|
|
335
|
+
api._middleware[method].push(middleware);
|
|
336
|
+
});
|
|
337
|
+
} else {
|
|
338
|
+
api._middleware['*'].push(middleware);
|
|
339
|
+
}
|
|
323
340
|
return this;
|
|
324
341
|
},
|
|
325
342
|
get(path: string, handler: HS.RouteHandler, handlerOptions?: HS.RouteHandlerOptions) {
|
package/src/types.ts
CHANGED
|
@@ -8,8 +8,8 @@ export namespace Hyperspan {
|
|
|
8
8
|
export interface Server {
|
|
9
9
|
_config: Hyperspan.Config;
|
|
10
10
|
_routes: Array<Hyperspan.Route>;
|
|
11
|
-
_middleware: Array<Hyperspan.MiddlewareFunction
|
|
12
|
-
use: (middleware: Hyperspan.MiddlewareFunction) => Hyperspan.Server;
|
|
11
|
+
_middleware: Record<Hyperspan.MiddlewareMethod, Array<Hyperspan.MiddlewareFunction>>;
|
|
12
|
+
use: (middleware: Hyperspan.MiddlewareFunction, opts?: Hyperspan.MiddlewareMethodOptions) => Hyperspan.Server;
|
|
13
13
|
get: (path: string, handler: Hyperspan.RouteHandler, handlerOptions?: Hyperspan.RouteHandlerOptions) => Hyperspan.Route;
|
|
14
14
|
post: (path: string, handler: Hyperspan.RouteHandler, handlerOptions?: Hyperspan.RouteHandlerOptions) => Hyperspan.Route;
|
|
15
15
|
put: (path: string, handler: Hyperspan.RouteHandler, handlerOptions?: Hyperspan.RouteHandlerOptions) => Hyperspan.Route;
|
|
@@ -133,11 +133,16 @@ export namespace Hyperspan {
|
|
|
133
133
|
context: Hyperspan.Context,
|
|
134
134
|
next: Hyperspan.NextFunction
|
|
135
135
|
) => Promise<Response> | Response;
|
|
136
|
+
export type MiddlewareMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS' | '*';
|
|
137
|
+
export type MiddlewareMethodOptions = {
|
|
138
|
+
methods?: Hyperspan.MiddlewareMethod[];
|
|
139
|
+
};
|
|
136
140
|
|
|
137
141
|
export interface Route {
|
|
138
142
|
_kind: 'hsRoute';
|
|
139
143
|
_config: Partial<Hyperspan.RouteConfig>;
|
|
140
144
|
_serverConfig?: Hyperspan.Config;
|
|
145
|
+
_middleware: Record<MiddlewareMethod, Array<Hyperspan.MiddlewareFunction>>;
|
|
141
146
|
_path(): string;
|
|
142
147
|
_methods(): string[];
|
|
143
148
|
get: (handler: Hyperspan.RouteHandler, handlerOptions?: Hyperspan.RouteHandlerOptions) => Hyperspan.Route;
|
|
@@ -148,8 +153,8 @@ export namespace Hyperspan {
|
|
|
148
153
|
options: (handler: Hyperspan.RouteHandler, handlerOptions?: Hyperspan.RouteHandlerOptions) => Hyperspan.Route;
|
|
149
154
|
all: (handler: Hyperspan.RouteHandler, handlerOptions?: Hyperspan.RouteHandlerOptions) => Hyperspan.Route;
|
|
150
155
|
errorHandler: (handler: Hyperspan.ErrorHandler) => Hyperspan.Route;
|
|
151
|
-
use: (middleware: Hyperspan.MiddlewareFunction) => Hyperspan.Route;
|
|
152
|
-
middleware: (middleware: Array<Hyperspan.MiddlewareFunction
|
|
156
|
+
use: (middleware: Hyperspan.MiddlewareFunction, opts?: Hyperspan.MiddlewareMethodOptions) => Hyperspan.Route;
|
|
157
|
+
middleware: (middleware: Array<Hyperspan.MiddlewareFunction>, opts?: Hyperspan.MiddlewareMethodOptions) => Hyperspan.Route;
|
|
153
158
|
fetch: (request: Request) => Promise<Response>;
|
|
154
159
|
};
|
|
155
160
|
|
|
@@ -178,8 +183,8 @@ export namespace Hyperspan {
|
|
|
178
183
|
render: (c: Context, props?: ActionFormProps<T>) => ActionFormResponse;
|
|
179
184
|
post: (handler: ActionFormHandler<T>) => Action<T>;
|
|
180
185
|
errorHandler: (handler: ActionFormHandler<T>) => Action<T>;
|
|
181
|
-
use: (middleware: Hyperspan.MiddlewareFunction) => Action<T>;
|
|
182
|
-
middleware: (middleware: Array<Hyperspan.MiddlewareFunction
|
|
186
|
+
use: (middleware: Hyperspan.MiddlewareFunction, opts?: Hyperspan.MiddlewareMethodOptions) => Action<T>;
|
|
187
|
+
middleware: (middleware: Array<Hyperspan.MiddlewareFunction>, opts?: Hyperspan.MiddlewareMethodOptions) => Action<T>;
|
|
183
188
|
fetch: (request: Request) => Promise<Response>;
|
|
184
189
|
}
|
|
185
190
|
|