HATEOAS-first backend framework.
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

server.ts 1.5 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import {
  2. application,
  3. resource,
  4. validation as v,
  5. mediaTypes,
  6. charsets,
  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. schema: v.number(),
  22. })
  23. .canFetchItem()
  24. .canFetchCollection()
  25. .canCreate()
  26. .canEmplace()
  27. .canPatch()
  28. .canDelete();
  29. const User = resource(v.object(
  30. {
  31. firstName: v.string(),
  32. middleName: v.string(),
  33. lastName: v.string(),
  34. bio: v.string(),
  35. birthday: v.datelike()
  36. },
  37. v.never()
  38. ))
  39. .id('id' as const, {
  40. generationStrategy: autoIncrement,
  41. serialize: (id) => id?.toString() ?? '0',
  42. deserialize: (id) => Number.isFinite(Number(id)) ? Number(id) : 0,
  43. schema: v.number(),
  44. })
  45. .name('User')
  46. .fullText('bio');
  47. const app = application({
  48. name: 'piano-service',
  49. })
  50. .mediaType(mediaTypes.applicationJson)
  51. .mediaType(mediaTypes.textJson)
  52. .mediaType(TEXT_SERIALIZER_PAIR)
  53. .charset(charsets.utf8)
  54. .resource(Piano)
  55. .resource(User);
  56. const backend = app.createBackend({
  57. dataSource,
  58. });
  59. const server = backend.createServer({
  60. basePath: '/api'
  61. });
  62. server.listen(3000);
  63. setTimeout(() => {
  64. // Allow user operations after 5 seconds from startup
  65. User
  66. .canFetchItem()
  67. .canFetchCollection()
  68. .canCreate()
  69. .canPatch();
  70. }, 5000);