HATEOAS-first backend framework.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

default.test.ts 2.1 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import {
  2. describe,
  3. beforeAll,
  4. afterAll,
  5. it,
  6. expect,
  7. } from 'vitest';
  8. import {app, endpoint, Endpoint, operation, Operation, validation as v} from '../../src/common';
  9. import {Server, backend} from '../../src/backend';
  10. import {Client} from '../../src/client';
  11. import {server, HttpResponse} from '../../src/extenders/http/backend';
  12. import {client} from '../../src/extenders/http/client';
  13. describe('default', () => {
  14. let theClient: Client;
  15. let theServer: Server;
  16. let theEndpoint: Endpoint;
  17. let theOperation: Operation;
  18. beforeAll(async () => {
  19. theOperation = operation({
  20. name: 'fetch' as const,
  21. });
  22. theEndpoint = endpoint({
  23. name: 'users' as const,
  24. schema: v.object({
  25. username: v.string(),
  26. }),
  27. })
  28. .param('resourceId');
  29. const theApp = app({
  30. name: 'default' as const,
  31. })
  32. .operation(theOperation)
  33. .endpoint(theEndpoint);
  34. const theBackend = backend({
  35. app: theApp,
  36. });
  37. theBackend.implementOperation('fetch', async (ctx) => {
  38. class YesResponse extends HttpResponse(204) {}
  39. return new YesResponse({
  40. statusMessage: 'Yes',
  41. }, {
  42. res: ctx.res,
  43. });
  44. });
  45. theServer = server({
  46. backend: theBackend,
  47. });
  48. const connectionParams = {
  49. port: 3001,
  50. };
  51. await theServer.serve(connectionParams);
  52. theClient = client({
  53. app: theApp,
  54. });
  55. await theClient.connect(connectionParams);
  56. });
  57. afterAll(async () => {
  58. await theClient.disconnect();
  59. await theServer.close();
  60. });
  61. it('works', async () => {
  62. theEndpoint.can('fetch');
  63. // TODO create wrapper for fetch's Response here
  64. //
  65. // should we create a helper object to process client-side received response from server's sent response?
  66. //
  67. // the motivation is to remove the manual deserialization from the client (provide serialization on the response
  68. // object so as the client is not limited to .text(), .json(), .arrayBuffer() etc)
  69. const response = await theClient
  70. .at(theEndpoint)
  71. .makeRequest(theOperation, new URLSearchParams({
  72. foo: 'bar',
  73. }));
  74. expect(response).toHaveProperty('status', 204);
  75. expect(response).toHaveProperty('statusText', 'Yes');
  76. });
  77. });