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.

server.ts 1.4 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import {
  2. application,
  3. resource,
  4. validation as v,
  5. } from '../../src';
  6. import {TEXT_SERIALIZER_PAIR} from './serializers';
  7. import {autoIncrement, dataSource} from './data-source';
  8. const Piano = resource(v.object(
  9. {
  10. brand: v.string()
  11. },
  12. v.never()
  13. ))
  14. .name('Piano')
  15. .route('pianos')
  16. .id('id', {
  17. generationStrategy: autoIncrement,
  18. serialize: (id) => id?.toString() ?? '0',
  19. deserialize: (id) => Number.isFinite(Number(id)) ? Number(id) : 0,
  20. schema: v.number(),
  21. })
  22. .canFetchItem()
  23. .canFetchCollection()
  24. .canCreate()
  25. .canEmplace()
  26. .canPatch()
  27. .canDelete();
  28. const User = resource(v.object(
  29. {
  30. firstName: v.string(),
  31. middleName: v.string(),
  32. lastName: v.string(),
  33. bio: v.string(),
  34. birthday: v.datelike()
  35. },
  36. v.never()
  37. ))
  38. .id('id' as const, {
  39. generationStrategy: autoIncrement,
  40. serialize: (id) => id?.toString() ?? '0',
  41. deserialize: (id) => Number.isFinite(Number(id)) ? Number(id) : 0,
  42. schema: v.number(),
  43. })
  44. .name('User')
  45. .route('users')
  46. .fullText('bio');
  47. const app = application({
  48. name: 'piano-service',
  49. })
  50. .mediaType(TEXT_SERIALIZER_PAIR)
  51. .resource(Piano)
  52. .resource(User);
  53. const backend = app.createBackend({
  54. dataSource,
  55. });
  56. const server = backend.createHttpServer({
  57. basePath: '/api'
  58. });
  59. server.listen(3000);
  60. setTimeout(() => {
  61. // Allow user operations after 5 seconds from startup
  62. User
  63. .canFetchItem()
  64. .canFetchCollection()
  65. .canCreate()
  66. .canPatch();
  67. }, 5000);