@ikatec/digisac-api-sdk 4.0.0 → 4.2.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 (224) hide show
  1. package/README.md +34 -4
  2. package/dist/apis/authHistory/AuthHistoryApi.cjs +11 -17
  3. package/dist/apis/authHistory/AuthHistoryApi.cjs.map +1 -1
  4. package/dist/apis/authHistory/AuthHistoryApi.d.ts +10 -18
  5. package/dist/apis/authHistory/AuthHistoryApi.d.ts.map +1 -1
  6. package/dist/apis/authHistory/AuthHistoryApi.mjs +11 -15
  7. package/dist/apis/authHistory/AuthHistoryApi.mjs.map +1 -1
  8. package/dist/apis/authHistory/types.d.ts +4 -3
  9. package/dist/apis/authHistory/types.d.ts.map +1 -1
  10. package/dist/apis/clientFeedback/ClientFeedbackApi.cjs +2 -5
  11. package/dist/apis/clientFeedback/ClientFeedbackApi.cjs.map +1 -1
  12. package/dist/apis/clientFeedback/ClientFeedbackApi.d.ts +2 -4
  13. package/dist/apis/clientFeedback/ClientFeedbackApi.d.ts.map +1 -1
  14. package/dist/apis/clientFeedback/ClientFeedbackApi.mjs +3 -5
  15. package/dist/apis/clientFeedback/ClientFeedbackApi.mjs.map +1 -1
  16. package/dist/apis/contacts/ContactsApi.cjs +2 -2
  17. package/dist/apis/contacts/ContactsApi.cjs.map +1 -1
  18. package/dist/apis/contacts/ContactsApi.mjs +2 -2
  19. package/dist/apis/contacts/ContactsApi.mjs.map +1 -1
  20. package/dist/apis/customFields/types.d.ts +21 -19
  21. package/dist/apis/customFields/types.d.ts.map +1 -1
  22. package/dist/apis/dashboard/DashboardApi.cjs +19 -0
  23. package/dist/apis/dashboard/DashboardApi.cjs.map +1 -0
  24. package/dist/apis/dashboard/DashboardApi.d.ts +12 -0
  25. package/dist/apis/dashboard/DashboardApi.d.ts.map +1 -0
  26. package/dist/apis/dashboard/DashboardApi.mjs +18 -0
  27. package/dist/apis/dashboard/DashboardApi.mjs.map +1 -0
  28. package/dist/apis/dashboard/index.cjs +4 -0
  29. package/dist/apis/dashboard/index.d.ts +3 -0
  30. package/dist/apis/dashboard/index.d.ts.map +1 -0
  31. package/dist/apis/dashboard/index.mjs +3 -0
  32. package/dist/apis/dashboard/types.cjs +0 -0
  33. package/dist/apis/dashboard/types.d.ts +53 -0
  34. package/dist/apis/dashboard/types.d.ts.map +1 -0
  35. package/dist/apis/dashboard/types.mjs +0 -0
  36. package/dist/apis/holiday/types.d.ts +34 -8
  37. package/dist/apis/holiday/types.d.ts.map +1 -1
  38. package/dist/apis/index.cjs +6 -0
  39. package/dist/apis/index.d.ts +2 -0
  40. package/dist/apis/index.d.ts.map +1 -1
  41. package/dist/apis/index.mjs +5 -1
  42. package/dist/apis/me/MeApi.cjs +2 -5
  43. package/dist/apis/me/MeApi.cjs.map +1 -1
  44. package/dist/apis/me/MeApi.d.ts +2 -4
  45. package/dist/apis/me/MeApi.d.ts.map +1 -1
  46. package/dist/apis/me/MeApi.mjs +3 -5
  47. package/dist/apis/me/MeApi.mjs.map +1 -1
  48. package/dist/apis/now/NowApi.cjs +37 -0
  49. package/dist/apis/now/NowApi.cjs.map +1 -0
  50. package/dist/apis/now/NowApi.d.ts +26 -0
  51. package/dist/apis/now/NowApi.d.ts.map +1 -0
  52. package/dist/apis/now/NowApi.mjs +36 -0
  53. package/dist/apis/now/NowApi.mjs.map +1 -0
  54. package/dist/apis/now/index.cjs +4 -0
  55. package/dist/apis/now/index.d.ts +3 -0
  56. package/dist/apis/now/index.d.ts.map +1 -0
  57. package/dist/apis/now/index.mjs +3 -0
  58. package/dist/apis/now/types.cjs +0 -0
  59. package/dist/apis/now/types.d.ts +45 -0
  60. package/dist/apis/now/types.d.ts.map +1 -0
  61. package/dist/apis/now/types.mjs +0 -0
  62. package/dist/apis/people/types.d.ts +7 -2
  63. package/dist/apis/people/types.d.ts.map +1 -1
  64. package/dist/apis/permissions/PermissionsApi.cjs +6 -2
  65. package/dist/apis/permissions/PermissionsApi.cjs.map +1 -1
  66. package/dist/apis/permissions/PermissionsApi.d.ts +7 -3
  67. package/dist/apis/permissions/PermissionsApi.d.ts.map +1 -1
  68. package/dist/apis/permissions/PermissionsApi.mjs +6 -2
  69. package/dist/apis/permissions/PermissionsApi.mjs.map +1 -1
  70. package/dist/apis/permissions/types.d.ts +4 -14
  71. package/dist/apis/permissions/types.d.ts.map +1 -1
  72. package/dist/apis/personalAccessTokens/PersonalAccessTokensApi.cjs +5 -0
  73. package/dist/apis/personalAccessTokens/PersonalAccessTokensApi.cjs.map +1 -1
  74. package/dist/apis/personalAccessTokens/PersonalAccessTokensApi.d.ts +5 -0
  75. package/dist/apis/personalAccessTokens/PersonalAccessTokensApi.d.ts.map +1 -1
  76. package/dist/apis/personalAccessTokens/PersonalAccessTokensApi.mjs +5 -0
  77. package/dist/apis/personalAccessTokens/PersonalAccessTokensApi.mjs.map +1 -1
  78. package/dist/apis/personalAccessTokens/types.d.ts +23 -4
  79. package/dist/apis/personalAccessTokens/types.d.ts.map +1 -1
  80. package/dist/apis/roles/RolesApi.cjs.map +1 -1
  81. package/dist/apis/roles/RolesApi.d.ts +2 -2
  82. package/dist/apis/roles/RolesApi.d.ts.map +1 -1
  83. package/dist/apis/roles/RolesApi.mjs.map +1 -1
  84. package/dist/apis/roles/types.d.ts +27 -4
  85. package/dist/apis/roles/types.d.ts.map +1 -1
  86. package/dist/apis/serviceAccessManagement/ServiceAccessManagementApi.cjs +3 -9
  87. package/dist/apis/serviceAccessManagement/ServiceAccessManagementApi.cjs.map +1 -1
  88. package/dist/apis/serviceAccessManagement/ServiceAccessManagementApi.d.ts +3 -4
  89. package/dist/apis/serviceAccessManagement/ServiceAccessManagementApi.d.ts.map +1 -1
  90. package/dist/apis/serviceAccessManagement/ServiceAccessManagementApi.mjs +3 -7
  91. package/dist/apis/serviceAccessManagement/ServiceAccessManagementApi.mjs.map +1 -1
  92. package/dist/apis/stickerUsers/StickerUsersApi.cjs +3 -9
  93. package/dist/apis/stickerUsers/StickerUsersApi.cjs.map +1 -1
  94. package/dist/apis/stickerUsers/StickerUsersApi.d.ts +3 -4
  95. package/dist/apis/stickerUsers/StickerUsersApi.d.ts.map +1 -1
  96. package/dist/apis/stickerUsers/StickerUsersApi.mjs +3 -7
  97. package/dist/apis/stickerUsers/StickerUsersApi.mjs.map +1 -1
  98. package/dist/apis/terms/TermsApi.cjs +7 -13
  99. package/dist/apis/terms/TermsApi.cjs.map +1 -1
  100. package/dist/apis/terms/TermsApi.d.ts +6 -5
  101. package/dist/apis/terms/TermsApi.d.ts.map +1 -1
  102. package/dist/apis/terms/TermsApi.mjs +7 -11
  103. package/dist/apis/terms/TermsApi.mjs.map +1 -1
  104. package/dist/apis/timetable/types.d.ts +12 -7
  105. package/dist/apis/timetable/types.d.ts.map +1 -1
  106. package/dist/apis/transcripts/TranscriptsApi.cjs +2 -5
  107. package/dist/apis/transcripts/TranscriptsApi.cjs.map +1 -1
  108. package/dist/apis/transcripts/TranscriptsApi.d.ts +2 -4
  109. package/dist/apis/transcripts/TranscriptsApi.d.ts.map +1 -1
  110. package/dist/apis/transcripts/TranscriptsApi.mjs +3 -5
  111. package/dist/apis/transcripts/TranscriptsApi.mjs.map +1 -1
  112. package/dist/core/ApiClient.d.ts +3 -0
  113. package/dist/core/ApiClient.d.ts.map +1 -1
  114. package/dist/core/ArchivableCrudApi.cjs +0 -2
  115. package/dist/core/ArchivableCrudApi.cjs.map +1 -1
  116. package/dist/core/ArchivableCrudApi.d.ts.map +1 -1
  117. package/dist/core/ArchivableCrudApi.mjs +0 -2
  118. package/dist/core/ArchivableCrudApi.mjs.map +1 -1
  119. package/dist/core/BaseApi.cjs +34 -0
  120. package/dist/core/BaseApi.cjs.map +1 -0
  121. package/dist/core/BaseApi.d.ts +20 -0
  122. package/dist/core/BaseApi.d.ts.map +1 -0
  123. package/dist/core/BaseApi.mjs +33 -0
  124. package/dist/core/BaseApi.mjs.map +1 -0
  125. package/dist/core/BaseApiClient.cjs +4 -1
  126. package/dist/core/BaseApiClient.cjs.map +1 -1
  127. package/dist/core/BaseApiClient.d.ts +7 -1
  128. package/dist/core/BaseApiClient.d.ts.map +1 -1
  129. package/dist/core/BaseApiClient.mjs +4 -1
  130. package/dist/core/BaseApiClient.mjs.map +1 -1
  131. package/dist/core/BaseCrudApi.cjs +3 -40
  132. package/dist/core/BaseCrudApi.cjs.map +1 -1
  133. package/dist/core/BaseCrudApi.d.ts +4 -23
  134. package/dist/core/BaseCrudApi.d.ts.map +1 -1
  135. package/dist/core/BaseCrudApi.mjs +3 -38
  136. package/dist/core/BaseCrudApi.mjs.map +1 -1
  137. package/dist/core/BaseReadApi.cjs +44 -0
  138. package/dist/core/BaseReadApi.cjs.map +1 -0
  139. package/dist/core/BaseReadApi.d.ts +32 -0
  140. package/dist/core/BaseReadApi.d.ts.map +1 -0
  141. package/dist/core/BaseReadApi.mjs +43 -0
  142. package/dist/core/BaseReadApi.mjs.map +1 -0
  143. package/dist/core/bindMethods.cjs +24 -0
  144. package/dist/core/bindMethods.cjs.map +1 -0
  145. package/dist/core/bindMethods.d.ts +7 -0
  146. package/dist/core/bindMethods.d.ts.map +1 -0
  147. package/dist/core/bindMethods.mjs +22 -0
  148. package/dist/core/bindMethods.mjs.map +1 -0
  149. package/dist/core/index.cjs +4 -0
  150. package/dist/core/index.d.ts +4 -0
  151. package/dist/core/index.d.ts.map +1 -1
  152. package/dist/core/index.mjs +3 -1
  153. package/dist/core/queryString.cjs +22 -0
  154. package/dist/core/queryString.cjs.map +1 -0
  155. package/dist/core/queryString.d.ts +14 -0
  156. package/dist/core/queryString.d.ts.map +1 -0
  157. package/dist/core/queryString.mjs +18 -0
  158. package/dist/core/queryString.mjs.map +1 -0
  159. package/dist/core/types.d.ts +2 -0
  160. package/dist/core/types.d.ts.map +1 -1
  161. package/dist/core/withQueryFormat.cjs +42 -0
  162. package/dist/core/withQueryFormat.cjs.map +1 -0
  163. package/dist/core/withQueryFormat.d.ts +10 -0
  164. package/dist/core/withQueryFormat.d.ts.map +1 -0
  165. package/dist/core/withQueryFormat.mjs +40 -0
  166. package/dist/core/withQueryFormat.mjs.map +1 -0
  167. package/dist/index.cjs +4 -0
  168. package/dist/index.mjs +3 -1
  169. package/package.json +1 -1
  170. package/src/apis/authHistory/AuthHistoryApi.test.ts +11 -3
  171. package/src/apis/authHistory/AuthHistoryApi.ts +11 -37
  172. package/src/apis/authHistory/types.ts +4 -3
  173. package/src/apis/campaigns/CampaignsApi.test.ts +1 -1
  174. package/src/apis/clientFeedback/ClientFeedbackApi.ts +5 -7
  175. package/src/apis/contacts/ContactsApi.test.ts +1 -1
  176. package/src/apis/contacts/ContactsApi.ts +2 -2
  177. package/src/apis/customFields/types.ts +21 -25
  178. package/src/apis/dashboard/DashboardApi.test.ts +82 -0
  179. package/src/apis/dashboard/DashboardApi.ts +18 -0
  180. package/src/apis/dashboard/index.ts +2 -0
  181. package/src/apis/dashboard/types.ts +53 -0
  182. package/src/apis/holiday/types.ts +36 -8
  183. package/src/apis/index.ts +2 -0
  184. package/src/apis/me/MeApi.ts +2 -8
  185. package/src/apis/messages/MessagesApi.test.ts +2 -2
  186. package/src/apis/now/NowApi.test.ts +125 -0
  187. package/src/apis/now/NowApi.ts +59 -0
  188. package/src/apis/now/index.ts +2 -0
  189. package/src/apis/now/types.ts +47 -0
  190. package/src/apis/payloads.test-d.ts +39 -0
  191. package/src/apis/payloads.test.ts +81 -0
  192. package/src/apis/people/types.ts +7 -2
  193. package/src/apis/permissions/PermissionsApi.test.ts +93 -0
  194. package/src/apis/permissions/PermissionsApi.ts +7 -7
  195. package/src/apis/permissions/types.ts +4 -16
  196. package/src/apis/personalAccessTokens/PersonalAccessTokensApi.ts +5 -0
  197. package/src/apis/personalAccessTokens/types.ts +25 -4
  198. package/src/apis/queryFormat.test.ts +273 -0
  199. package/src/apis/roles/RolesApi.ts +7 -2
  200. package/src/apis/roles/types.ts +27 -4
  201. package/src/apis/serviceAccessManagement/ServiceAccessManagementApi.test.ts +2 -2
  202. package/src/apis/serviceAccessManagement/ServiceAccessManagementApi.ts +10 -10
  203. package/src/apis/stickerUsers/StickerUsersApi.test.ts +1 -1
  204. package/src/apis/stickerUsers/StickerUsersApi.ts +7 -10
  205. package/src/apis/terms/TermsApi.ts +7 -13
  206. package/src/apis/timetable/types.ts +13 -7
  207. package/src/apis/transcripts/TranscriptsApi.ts +2 -7
  208. package/src/core/ApiClient.ts +4 -0
  209. package/src/core/ArchivableCrudApi.ts +0 -2
  210. package/src/core/BaseApi.test.ts +164 -0
  211. package/src/core/BaseApi.ts +32 -0
  212. package/src/core/BaseApiClient.test.ts +10 -0
  213. package/src/core/BaseApiClient.ts +9 -1
  214. package/src/core/BaseCrudApi.test-d.ts +9 -0
  215. package/src/core/BaseCrudApi.test.ts +131 -60
  216. package/src/core/BaseCrudApi.ts +7 -63
  217. package/src/core/BaseReadApi.ts +70 -0
  218. package/src/core/bindMethods.ts +26 -0
  219. package/src/core/index.ts +4 -0
  220. package/src/core/queryString.test.ts +71 -0
  221. package/src/core/queryString.ts +30 -0
  222. package/src/core/types.ts +5 -0
  223. package/src/core/withQueryFormat.test.ts +136 -0
  224. package/src/core/withQueryFormat.ts +47 -0
@@ -0,0 +1,136 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2
+ import type { ApiClient } from './ApiClient'
3
+ import { BaseApiClient } from './BaseApiClient'
4
+ import { BaseCrudApi } from './BaseCrudApi'
5
+ import { StickerUsersApi } from '../apis/stickerUsers/StickerUsersApi'
6
+ import { TermsApi } from '../apis/terms/TermsApi'
7
+
8
+ type Item = { id: string; name: string }
9
+
10
+ class ItemsApi extends BaseCrudApi<Item, { name: string }> {
11
+ constructor(client: ApiClient) {
12
+ super(client, '/items')
13
+ this.searchByName = this.searchByName.bind(this)
14
+ }
15
+
16
+ searchByName(name: string) {
17
+ return this.getMany({ where: { name }, paginate: false })
18
+ }
19
+ }
20
+
21
+ function createMockClient(): ApiClient {
22
+ return {
23
+ setAccessToken: vi.fn().mockReturnThis(),
24
+ request: vi.fn(),
25
+ get: vi.fn().mockResolvedValue([]),
26
+ post: vi.fn(),
27
+ put: vi.fn(),
28
+ patch: vi.fn(),
29
+ delete: vi.fn(),
30
+ }
31
+ }
32
+
33
+ const calledUrl = (client: ApiClient, call = 0) =>
34
+ decodeURIComponent(vi.mocked(client.get).mock.calls.at(call)?.at(0) as string)
35
+
36
+ const JSON_QUERY = '/items?query={"where":{"name":"x"},"paginate":false}'
37
+ const QS_QUERY = '/items?where[name]=x&paginate=false'
38
+
39
+ describe('withQueryFormat', () => {
40
+ let client: ApiClient
41
+ let api: ItemsApi
42
+
43
+ beforeEach(() => {
44
+ client = createMockClient()
45
+ api = new ItemsApi(client)
46
+ })
47
+
48
+ it('returns a copy that uses the given format', async () => {
49
+ await api.withQueryFormat('qs').getMany({ where: { name: 'x' }, paginate: false })
50
+ expect(calledUrl(client)).toBe(QS_QUERY)
51
+ })
52
+
53
+ it('leaves the original API unchanged', async () => {
54
+ api.withQueryFormat('qs')
55
+ await api.getMany({ where: { name: 'x' }, paginate: false })
56
+ expect(calledUrl(client)).toBe(JSON_QUERY)
57
+ expect(api.client).toBe(client)
58
+ })
59
+
60
+ it('keeps the class, basePath and fields', () => {
61
+ const copy = api.withQueryFormat('qs')
62
+ expect(copy).toBeInstanceOf(ItemsApi)
63
+ expect(copy).toBeInstanceOf(BaseCrudApi)
64
+ expect(copy.basePath).toBe('/items')
65
+ expect(copy).not.toBe(api)
66
+ })
67
+
68
+ it('binds destructured methods to the copy', async () => {
69
+ const { getMany, getOne, getById } = api.withQueryFormat('qs')
70
+ await getMany({ where: { name: 'x' }, paginate: false })
71
+ await getOne({ where: { name: 'x' } })
72
+ await getById('1', { attributes: ['id'] })
73
+ expect(calledUrl(client, 0)).toBe(QS_QUERY)
74
+ expect(calledUrl(client, 1)).toBe('/items?where[name]=x&limit=1&paginate=false')
75
+ expect(calledUrl(client, 2)).toBe('/items/1?attributes[0]=id')
76
+ })
77
+
78
+ it('rebinds methods bound by subclass constructors', async () => {
79
+ const { searchByName } = api.withQueryFormat('qs')
80
+ await searchByName('x')
81
+ expect(calledUrl(client)).toBe(QS_QUERY)
82
+ })
83
+
84
+ it('can be chained again', async () => {
85
+ await api.withQueryFormat('qs').withQueryFormat('json').searchByName('x')
86
+ expect(calledUrl(client)).toBe(JSON_QUERY)
87
+ })
88
+
89
+ it('overrides a qs client back to json', async () => {
90
+ const qsApi = new ItemsApi(Object.assign(createMockClient(), { queryFormat: 'qs' as const }))
91
+ await qsApi.withQueryFormat('json').searchByName('x')
92
+ expect(calledUrl(qsApi.client)).toBe(JSON_QUERY)
93
+ })
94
+
95
+ it('still sends requests through the original client', async () => {
96
+ const copy = api.withQueryFormat('qs')
97
+ await copy.create({ name: 'x' })
98
+ expect(client.post).toHaveBeenCalledWith('/items', { name: 'x' }, undefined)
99
+ expect(copy.client.queryFormat).toBe('qs')
100
+ expect(client.queryFormat).toBeUndefined()
101
+ })
102
+
103
+ it('works on APIs that do not extend BaseCrudApi', async () => {
104
+ await new StickerUsersApi(client).withQueryFormat('qs').getMany({ page: 2 })
105
+ await new TermsApi(client).withQueryFormat('qs').getById('t1', { attributes: ['id'] })
106
+ expect(calledUrl(client, 0)).toBe('/sticker-users?page=2')
107
+ expect(calledUrl(client, 1)).toBe('/terms/t1?attributes[0]=id')
108
+ })
109
+
110
+ describe('with BaseApiClient', () => {
111
+ afterEach(() => {
112
+ vi.restoreAllMocks()
113
+ })
114
+
115
+ const mockFetch = () =>
116
+ vi
117
+ .spyOn(globalThis, 'fetch')
118
+ .mockResolvedValue(
119
+ new Response('[]', { status: 200, headers: { 'Content-Type': 'application/json' } }),
120
+ )
121
+
122
+ it('uses the base URL and a token set on the original client after copying', async () => {
123
+ const fetchSpy = mockFetch()
124
+ const baseClient = new BaseApiClient('https://api.example.com', 'old-token')
125
+ const copy = new ItemsApi(baseClient).withQueryFormat('qs')
126
+ baseClient.setAccessToken('new-token')
127
+
128
+ await copy.searchByName('x')
129
+
130
+ const [url, init] = fetchSpy.mock.calls[0] as [string, RequestInit]
131
+ expect(decodeURIComponent(url)).toBe(`https://api.example.com${QS_QUERY}`)
132
+ expect((init.headers as Record<string, string>).Authorization).toBe('Bearer new-token')
133
+ expect(baseClient.queryFormat).toBe('json')
134
+ })
135
+ })
136
+ })
@@ -0,0 +1,47 @@
1
+ import type { ApiClient } from './ApiClient'
2
+ import type { QueryFormat } from './queryString'
3
+
4
+ /**
5
+ * A view of `client` that serializes queries in `format`. Everything else — methods, base URL and
6
+ * the access token — is read from `client`, so `client.setAccessToken()` still applies to it.
7
+ */
8
+ function overrideQueryFormat<T extends ApiClient>(client: T, format: QueryFormat): T {
9
+ return Object.create(client, {
10
+ queryFormat: { value: format, enumerable: true, configurable: true },
11
+ })
12
+ }
13
+
14
+ /**
15
+ * Copy of `api` whose client serializes queries in `format`; `api` itself is untouched.
16
+ *
17
+ * The copy has the same prototype and fields as `api`, with `client` replaced. Methods the
18
+ * constructor bound to `api` (`this.getMany = this.getMany.bind(this)`) are bound again to the
19
+ * copy, so they use its client.
20
+ */
21
+ export function cloneWithQueryFormat<T extends object>(api: T, format: QueryFormat): T {
22
+ const source = api as T & { client: ApiClient }
23
+ const prototype = Object.getPrototypeOf(api) as Record<PropertyKey, unknown>
24
+ const copy = Object.create(prototype) as Record<PropertyKey, unknown>
25
+
26
+ for (const key of Reflect.ownKeys(api)) {
27
+ Object.defineProperty(
28
+ copy,
29
+ key,
30
+ Object.getOwnPropertyDescriptor(api, key) as PropertyDescriptor,
31
+ )
32
+ }
33
+ Object.defineProperty(copy, 'client', {
34
+ value: overrideQueryFormat(source.client, format),
35
+ enumerable: true,
36
+ configurable: true,
37
+ writable: false,
38
+ })
39
+ for (const key of Reflect.ownKeys(api)) {
40
+ const method = prototype[key]
41
+ if (typeof copy[key] === 'function' && typeof method === 'function') {
42
+ copy[key] = method.bind(copy)
43
+ }
44
+ }
45
+
46
+ return copy as T
47
+ }