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.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import {
  2. application,
  3. resource,
  4. valibot as v,
  5. serializers,
  6. encodings,
  7. } from '../../src';
  8. import {TEXT_SERIALIZER_PAIR} from './serializers';
  9. import {autoIncrement, dataSource} from './data-source';
  10. const Piano = resource(v.object(
  11. {
  12. brand: v.string()
  13. },
  14. v.never()
  15. ))
  16. .name('Piano')
  17. .id('id', {
  18. generationStrategy: autoIncrement,
  19. serialize: (id) => id?.toString() ?? '0',
  20. deserialize: (id) => Number.isFinite(Number(id)) ? Number(id) : 0,
  21. })
  22. .allowFetchItem()
  23. .allowFetchCollection()
  24. .allowCreate()
  25. .allowEmplace()
  26. .allowPatch()
  27. .allowDelete();
  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. .name('User')
  39. .fullText('bio')
  40. .id('id', {
  41. generationStrategy: autoIncrement,
  42. serialize: (id) => id?.toString() ?? '0',
  43. deserialize: (id) => Number.isFinite(Number(id)) ? Number(id) : 0,
  44. });
  45. const app = application({
  46. name: 'piano-service',
  47. dataSource,
  48. })
  49. .contentType('application/json', serializers.applicationJson)
  50. .contentType('text/json', serializers.textJson)
  51. .contentType('text/plain', TEXT_SERIALIZER_PAIR)
  52. .encoding('utf-8', encodings.utf8)
  53. .resource(Piano)
  54. .resource(User);
  55. const server = app.createServer({
  56. baseUrl: '/api'
  57. });
  58. server.listen(3000);
  59. setTimeout(() => {
  60. // Allow user operations after 5 seconds from startup
  61. User
  62. .allowFetchItem()
  63. .allowFetchCollection()
  64. .allowCreate()
  65. .allowPatch();
  66. }, 5000);