handlers.js/test/test-server.ts

65 lines
1.3 KiB
TypeScript
Raw Permalink Normal View History

import * as handlersJS from '../index';
const App = new handlersJS.rootRouter();
2024-07-19 04:38:11 +00:00
App.binding(
'/',
App.create('GET', async () => '200 OK'),
);
2024-07-31 20:58:14 +00:00
const dynamicHandler = new handlersJS.handler('GET', [
async () => new handlersJS.response('miss'),
]);
App.binding('/handler', dynamicHandler);
App.binding(
'/handler/add',
App.create('GET', async () => {
dynamicHandler.add(async () => new handlersJS.response('hit'));
return 'added';
}),
);
2024-07-19 04:38:11 +00:00
App.binding(
'/post',
App.create(
'POST',
async (request: handlersJS.request<any>) => request.body,
),
);
2024-07-19 04:38:11 +00:00
App.binding(
'/header',
App.create('GET', async () => {
const response = new handlersJS.response<any>('');
response.status = 204;
response.headers.set('itis', 'work');
return response;
}),
);
2024-07-19 04:38:11 +00:00
App.route('/info/(.*)')
.binding(
'/foo',
App.create(
'GET',
(): Promise<handlersJS.response<any>> =>
2024-08-01 05:26:50 +00:00
new Promise(() => {
2024-07-19 04:38:11 +00:00
throw new handlersJS.response('hit');
}),
),
)
.binding(
'/(.*)',
App.create(
'GET',
async (request: handlersJS.request<any>) =>
request.params[0] ?? 'not found',
),
);
App.useMappingAdapter();
2024-07-19 04:38:11 +00:00
export default App;