@datocms/rest-client-utils 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +1 -0
  3. package/dist/cjs/ApiError.js +138 -0
  4. package/dist/cjs/ApiError.js.map +1 -0
  5. package/dist/cjs/CancelablePromise.js +53 -0
  6. package/dist/cjs/CancelablePromise.js.map +1 -0
  7. package/dist/cjs/deserialize.js +57 -0
  8. package/dist/cjs/deserialize.js.map +1 -0
  9. package/dist/cjs/index.js +26 -0
  10. package/dist/cjs/index.js.map +1 -0
  11. package/dist/cjs/internalTypes.js +3 -0
  12. package/dist/cjs/internalTypes.js.map +1 -0
  13. package/dist/cjs/pollJobResult.js +75 -0
  14. package/dist/cjs/pollJobResult.js.map +1 -0
  15. package/dist/cjs/rawPageIterator.js +152 -0
  16. package/dist/cjs/rawPageIterator.js.map +1 -0
  17. package/dist/cjs/request.js +283 -0
  18. package/dist/cjs/request.js.map +1 -0
  19. package/dist/cjs/serialize.js +142 -0
  20. package/dist/cjs/serialize.js.map +1 -0
  21. package/dist/cjs/toId.js +8 -0
  22. package/dist/cjs/toId.js.map +1 -0
  23. package/dist/cjs/wait.js +10 -0
  24. package/dist/cjs/wait.js.map +1 -0
  25. package/dist/esm/ApiError.d.ts +34 -0
  26. package/dist/esm/ApiError.js +135 -0
  27. package/dist/esm/ApiError.js.map +1 -0
  28. package/dist/esm/CancelablePromise.d.ts +7 -0
  29. package/dist/esm/CancelablePromise.js +49 -0
  30. package/dist/esm/CancelablePromise.js.map +1 -0
  31. package/dist/esm/deserialize.d.ts +10 -0
  32. package/dist/esm/deserialize.js +52 -0
  33. package/dist/esm/deserialize.js.map +1 -0
  34. package/dist/esm/index.d.ts +9 -0
  35. package/dist/esm/index.js +11 -0
  36. package/dist/esm/index.js.map +1 -0
  37. package/dist/esm/internalTypes.d.ts +8 -0
  38. package/dist/esm/internalTypes.js +2 -0
  39. package/dist/esm/internalTypes.js.map +1 -0
  40. package/dist/esm/pollJobResult.d.ts +2 -0
  41. package/dist/esm/pollJobResult.js +71 -0
  42. package/dist/esm/pollJobResult.js.map +1 -0
  43. package/dist/esm/rawPageIterator.d.ts +19 -0
  44. package/dist/esm/rawPageIterator.js +148 -0
  45. package/dist/esm/rawPageIterator.js.map +1 -0
  46. package/dist/esm/request.d.ts +28 -0
  47. package/dist/esm/request.js +276 -0
  48. package/dist/esm/request.js.map +1 -0
  49. package/dist/esm/serialize.d.ts +22 -0
  50. package/dist/esm/serialize.js +138 -0
  51. package/dist/esm/serialize.js.map +1 -0
  52. package/dist/esm/toId.d.ts +3 -0
  53. package/dist/esm/toId.js +4 -0
  54. package/dist/esm/toId.js.map +1 -0
  55. package/dist/esm/wait.d.ts +1 -0
  56. package/dist/esm/wait.js +6 -0
  57. package/dist/esm/wait.js.map +1 -0
  58. package/dist/types/ApiError.d.ts +34 -0
  59. package/dist/types/CancelablePromise.d.ts +7 -0
  60. package/dist/types/deserialize.d.ts +10 -0
  61. package/dist/types/index.d.ts +9 -0
  62. package/dist/types/internalTypes.d.ts +8 -0
  63. package/dist/types/pollJobResult.d.ts +2 -0
  64. package/dist/types/rawPageIterator.d.ts +19 -0
  65. package/dist/types/request.d.ts +28 -0
  66. package/dist/types/serialize.d.ts +22 -0
  67. package/dist/types/toId.d.ts +3 -0
  68. package/dist/types/wait.d.ts +1 -0
  69. package/package.json +44 -0
  70. package/src/ApiError.ts +167 -0
  71. package/src/CancelablePromise.ts +41 -0
  72. package/src/deserialize.ts +52 -0
  73. package/src/index.ts +11 -0
  74. package/src/internalTypes.ts +8 -0
  75. package/src/pollJobResult.ts +24 -0
  76. package/src/rawPageIterator.ts +66 -0
  77. package/src/request.ts +279 -0
  78. package/src/serialize.ts +115 -0
  79. package/src/toId.ts +3 -0
  80. package/src/wait.ts +5 -0
@@ -0,0 +1,115 @@
1
+ type Options =
2
+ | {
3
+ id?: string;
4
+ type: string;
5
+ attributes: '*';
6
+ relationships: string[];
7
+ }
8
+ | {
9
+ id?: string;
10
+ type: string;
11
+ attributes: string[];
12
+ relationships: '*';
13
+ }
14
+ | {
15
+ id?: string;
16
+ type: string;
17
+ attributes: string[];
18
+ relationships: string[];
19
+ };
20
+
21
+ export type Rel = {
22
+ id: string;
23
+ type: string;
24
+ };
25
+
26
+ function isRel(object: unknown): object is Rel {
27
+ return (
28
+ typeof object === 'object' && !!object && 'id' in object && 'type' in object
29
+ );
30
+ }
31
+
32
+ function isRelArray(object: unknown): object is Rel[] {
33
+ return Array.isArray(object) && object.every(isRel);
34
+ }
35
+
36
+ export function serializeRequestBody<T>(body: unknown, options: Options): T {
37
+ if (typeof body !== 'object' || !body) {
38
+ throw new Error('Invalid body!');
39
+ }
40
+
41
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
42
+ const { id, type, meta, ...otherProperties } = body as any;
43
+
44
+ const attributes = {};
45
+ const relationships = {};
46
+
47
+ if (options.attributes === '*') {
48
+ Object.entries(otherProperties).forEach(([key, value]) => {
49
+ if (options.relationships.includes(key)) {
50
+ if (isRel(value)) {
51
+ const { id, type } = value;
52
+ relationships[key] = { data: { id, type } };
53
+ } else if (isRelArray(value)) {
54
+ relationships[key] = {
55
+ data: value.map(({ id, type }) => ({ id, type })),
56
+ };
57
+ } else {
58
+ relationships[key] = {
59
+ data: null,
60
+ };
61
+ }
62
+ } else {
63
+ attributes[key] = value;
64
+ }
65
+ });
66
+ } else if (options.relationships === '*') {
67
+ Object.entries(otherProperties).forEach(([key, value]) => {
68
+ if (options.attributes.includes(key)) {
69
+ attributes[key] = value;
70
+ } else {
71
+ if (isRel(value)) {
72
+ const { id, type } = value;
73
+ relationships[key] = { data: { id, type } };
74
+ } else if (isRelArray(value)) {
75
+ relationships[key] = {
76
+ data: value.map(({ id, type }) => ({ id, type })),
77
+ };
78
+ } else {
79
+ relationships[key] = {
80
+ data: null,
81
+ };
82
+ }
83
+ }
84
+ });
85
+ } else {
86
+ Object.entries(otherProperties).forEach(([key, value]) => {
87
+ if (options.attributes.includes(key)) {
88
+ attributes[key] = value;
89
+ } else if (options.relationships.includes(key)) {
90
+ if (isRel(value)) {
91
+ const { id, type } = value;
92
+ relationships[key] = { data: { id, type } };
93
+ } else if (isRelArray(value)) {
94
+ relationships[key] = {
95
+ data: value.map(({ id, type }) => ({ id, type })),
96
+ };
97
+ } else {
98
+ relationships[key] = {
99
+ data: null,
100
+ };
101
+ }
102
+ }
103
+ });
104
+ }
105
+
106
+ return {
107
+ data: {
108
+ ...(id || options.id ? { id: id || options.id } : {}),
109
+ type: type || options.type,
110
+ ...(Object.keys(attributes).length > 0 ? { attributes } : {}),
111
+ ...(Object.keys(relationships).length > 0 ? { relationships } : {}),
112
+ ...(meta ? { meta } : {}),
113
+ },
114
+ } as unknown as T;
115
+ }
package/src/toId.ts ADDED
@@ -0,0 +1,3 @@
1
+ export function toId(thing: string | { id: string }): string {
2
+ return typeof thing === 'string' ? thing : thing.id;
3
+ }
package/src/wait.ts ADDED
@@ -0,0 +1,5 @@
1
+ export function wait(time) {
2
+ return new Promise((resolve) => {
3
+ setTimeout(resolve, time);
4
+ });
5
+ }