|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- import {describe, it, expect, beforeAll, afterAll} from 'vitest';
- import {
- App,
- app,
- AppOperations,
- DataSource,
- Endpoint,
- endpoint, EndpointOperations,
- Operation,
- operation,
- validation as v,
- } from '../src/common';
- import {Backend, backend, Server} from '../src/backend';
- import {Client} from '../src/client';
- import {server} from '../src/extenders/http/backend/core';
- import {client} from '../src/extenders/http/client';
-
- const op = operation({
- name: 'create' as const,
- method: 'POST' as const,
- });
-
- const e = endpoint({
- name: 'e' as const,
- schema: v.object({}),
- })
- .can('create');
-
- const a = app({
- name: 'foo' as const,
- })
- .operation(op)
- .endpoint(e);
-
- describe('app', () => {
- let theApp: App;
- let theBackend: Backend;
- let theClient: Client;
- let theDataSource: DataSource;
- let theEndpoint: Endpoint;
- let theServer: Server;
- 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')
- .can('fetch');
-
- theApp = app({
- name: 'foo' as const
- })
- .operation(theOperation)
- .endpoint(theEndpoint);
-
- theBackend = backend({
- app: theApp
- });
-
- // add recipes function that will wrap app and backend to add operations and implement them, and will return a set
- // of operations.
- //
- // recipes should have a backend and client counterpart.
- theBackend.implementOperation('fetch', async (ctx) => {
- // noop
- });
-
- theServer = server({
- backend: theBackend
- });
-
- const connectionParams = {
- port: 3000,
- };
-
- await theServer.serve(connectionParams);
-
- theClient = client({
- app: theApp
- });
-
- await theClient.connect(connectionParams);
- });
-
- afterAll(async () => {
- await theClient.disconnect();
- await theServer.close();
- });
-
- it('works', async () => {
- const response = await theClient
- .at(theEndpoint, { resourceId: 3 })
- .makeRequest(theOperation);
-
- expect(response).toHaveProperty('status', 422);
- });
- });
-
- // const theEndpoint = endpoint({
- // schema: v.object({
- // username: v.string(),
- // }),
- // })
- // .can('patch')
- // .can('query');
- //
- // const canPatch = operation({
- // name: 'patch' as const,
- // args: [
- // 'merge',
- // 'delta',
- // ] as const,
- // // TODO define resource-specific stuff, like defining URL params, etc.
- // });
- //
- // const canFetch = operation({
- // name: 'fetch' as const,
- // args: [
- // 'item',
- // 'default',
- // ] as const,
- // });
- //
- // const canQuery = operation({
- // name: 'query' as const,
- // });
- //
- // const canCreate = operation({
- // name: 'create' as const,
- // });
- //
- // const canEmplace = operation({
- // name: 'emplace' as const,
- // });
- //
- // const canDelete = operation({
- // name: 'delete' as const,
- // });
- //
- // export const theApp = app({
- // name: 'foo' as const,
- // })
- // .operation(canQuery)
- // .operation(canPatch)
- // .operation(canFetch)
- // .operation(canCreate)
- // .operation(canEmplace)
- // .operation(canDelete)
- // .endpoint(theEndpoint);
- // //
- // // const bootstrap = async (theApp: App) => {
- // // if (typeof window === 'undefined') {
- // // const { backend } = await import('./backend');
- // // const theBackend = backend({
- // // app: theApp
- // // });
- // // }
- // // };
- //
- // const b = backend({
- // app: theApp,
- // })
- // .implementOperation({
- // operation: 'fetch' as const,
- // implementation: ({
- // endpoint,
- // arg
- // }) => {
- // switch (arg) {
- // case 'default': {
- //
- // }
- // }
- // },
- // });
- //
- // const s = server({
- // backend: b,
- // })
- // .serve({
- // host: '0.0.0.0',
- // port: 3000,
- // basePath: '/api'
- // });
|