- import {
- describe,
- beforeAll,
- afterAll,
- it,
- expect,
- } from 'vitest';
-
- import {
- app,
- Endpoint,
- Operation,
- } from '../../src/common';
- import {Server} from '../../src/backend';
- import {Client} from '../../src/client';
- import {server} from '../../src/extenders/http/backend';
- import {client} from '../../src/extenders/http/client';
- import {composeRecipes} from '../../src/common/recipe';
- import {addResourceRecipe, YesResponse} from '../../src/recipes/resource';
-
- describe('default', () => {
- let theClient: Client;
- let theServer: Server;
- let theRawEndpoint: Endpoint;
- let theOperation: Operation;
-
- beforeAll(async () => {
- const theRawApp = app({
- name: 'default' as const,
- });
-
- const {
- app: theApp,
- operations,
- backend: theBackend,
- } = composeRecipes([
- addResourceRecipe({ endpointName: 'users' }),
- addResourceRecipe({ endpointName: 'posts' })
- ])({
- app: theRawApp,
- });
-
- theRawEndpoint = Array.from(theApp.endpoints).find((e) => e.name === 'users');
- theOperation = operations.fetch;
-
- 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 () => {
- // 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 responseRaw = await theClient
- .at(theRawEndpoint)
- .makeRequest(theOperation, new URLSearchParams({
- foo: 'bar',
- }));
-
- const response = YesResponse.fromFetchResponse(responseRaw);
-
- expect(response).toHaveProperty('statusCode', 204);
- expect(response).toHaveProperty('statusMessage', 'Yes');
- });
- });
|