|
- import {
- describe,
- beforeAll,
- afterAll,
- it,
- expect,
- } from 'vitest';
-
- import {app, endpoint, Endpoint, operation, Operation, validation as v} from '../../src/common';
- import {Server, backend} from '../../src/backend';
- import {Client} from '../../src/client';
- import {server, HttpResponse} from '../../src/extenders/http/backend';
- import {client} from '../../src/extenders/http/client';
-
- describe('default', () => {
- let theClient: Client;
- let theServer: Server;
- let theEndpoint: Endpoint;
- let theOperation: Operation;
-
- beforeAll(async () => {
- theOperation = operation({
- name: 'fetch' as const,
- });
-
- theEndpoint = endpoint({
- name: 'users' as const,
- schema: v.object({
- username: v.string(),
- }),
- })
- .param('resourceId');
-
- const theApp = app({
- name: 'default' as const,
- })
- .operation(theOperation)
- .endpoint(theEndpoint);
-
- const theBackend = backend({
- app: theApp,
- });
-
- theBackend.implementOperation('fetch', async (ctx) => {
- class YesResponse extends HttpResponse(204) {}
-
- return new YesResponse({
- statusMessage: 'Yes',
- }, {
- res: ctx.res,
- });
- });
-
- theServer = server({
- backend: theBackend,
- });
-
- const connectionParams = {
- port: 3001,
- };
-
- await theServer.serve(connectionParams);
-
- theClient = client({
- app: theApp,
- });
-
- await theClient.connect(connectionParams);
- });
-
- afterAll(async () => {
- await theClient.disconnect();
- await theServer.close();
- });
-
- it('works', async () => {
- theEndpoint.can('fetch');
-
- // TODO create wrapper for fetch's Response here
- //
- // should we create a helper object to process client-side received response from server's sent response?
- //
- // the motivation is to remove the manual deserialization from the client (provide serialization on the response
- // object so as the client is not limited to .text(), .json(), .arrayBuffer() etc)
- const response = await theClient
- .at(theEndpoint)
- .makeRequest(theOperation, new URLSearchParams({
- foo: 'bar',
- }));
-
- expect(response).toHaveProperty('status', 204);
- expect(response).toHaveProperty('statusText', 'Yes');
- });
- });
|