@rundit-sdk/embed 0.0.1

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.
package/dist/index.js ADDED
@@ -0,0 +1,326 @@
1
+ const routeManifest = {
2
+ "companies": {
3
+ "getOne": {
4
+ "method": "GET",
5
+ "path": "/companies/:id",
6
+ "summary": "Get one company available to the SDK consumer",
7
+ "description": null,
8
+ "exampleCall": "client.companies.getOne('123')",
9
+ "responseType": "CompaniesGetOneResponse",
10
+ "pathParams": [
11
+ {
12
+ "name": "id",
13
+ "type": "string",
14
+ "description": "Company identifier"
15
+ }
16
+ ],
17
+ "queryParams": []
18
+ },
19
+ "getAll": {
20
+ "method": "GET",
21
+ "path": "/companies",
22
+ "summary": "List companies available to the SDK consumer",
23
+ "description": null,
24
+ "exampleCall": "client.companies.getAll()",
25
+ "responseType": "CompaniesGetAllResponse",
26
+ "pathParams": [],
27
+ "queryParams": []
28
+ }
29
+ },
30
+ "companyGroups": {
31
+ "getAll": {
32
+ "method": "GET",
33
+ "path": "/company-groups",
34
+ "summary": "List company groups available to the SDK consumer",
35
+ "description": null,
36
+ "exampleCall": "client.companyGroups.getAll()",
37
+ "responseType": "CompanyGroupsGetAllResponse",
38
+ "pathParams": [],
39
+ "queryParams": []
40
+ },
41
+ "getOne": {
42
+ "method": "GET",
43
+ "path": "/company-groups/:id",
44
+ "summary": "Get one company group available to the SDK consumer",
45
+ "description": null,
46
+ "exampleCall": "client.companyGroups.getOne('123')",
47
+ "responseType": "CompanyGroupsGetOneResponse",
48
+ "pathParams": [
49
+ {
50
+ "name": "id",
51
+ "type": "string",
52
+ "description": "Company group identifier"
53
+ }
54
+ ],
55
+ "queryParams": []
56
+ }
57
+ },
58
+ "positions": {
59
+ "getCompanyPositions": {
60
+ "method": "GET",
61
+ "path": "/positions/companies/:id",
62
+ "summary": "Get positions for one company",
63
+ "description": null,
64
+ "exampleCall": "client.positions.getCompanyPositions('123', { currency: 'USD' })",
65
+ "responseType": "PositionsGetCompanyPositionsResponse",
66
+ "pathParams": [
67
+ {
68
+ "name": "id",
69
+ "type": "string",
70
+ "description": "Company identifier"
71
+ }
72
+ ],
73
+ "queryParams": [
74
+ {
75
+ "name": "companyGroupId",
76
+ "required": false,
77
+ "type": "number",
78
+ "description": "Optional company group identifier to filter by"
79
+ },
80
+ {
81
+ "name": "currency",
82
+ "required": true,
83
+ "type": "string",
84
+ "description": "Reporting currency code"
85
+ },
86
+ {
87
+ "name": "date",
88
+ "required": false,
89
+ "type": "string",
90
+ "description": "Optional summary date in ISO format"
91
+ }
92
+ ]
93
+ },
94
+ "getPortfolioPositions": {
95
+ "method": "GET",
96
+ "path": "/positions/portfolio",
97
+ "summary": "Get portfolio positions",
98
+ "description": null,
99
+ "exampleCall": "client.positions.getPortfolioPositions({ currency: 'USD' })",
100
+ "responseType": "PositionsGetPortfolioPositionsResponse",
101
+ "pathParams": [],
102
+ "queryParams": [
103
+ {
104
+ "name": "companyGroupIds",
105
+ "required": false,
106
+ "type": "number[]",
107
+ "description": "Optional list of company group identifiers to filter the portfolio positions"
108
+ },
109
+ {
110
+ "name": "currency",
111
+ "required": true,
112
+ "type": "string",
113
+ "description": "Reporting currency code"
114
+ },
115
+ {
116
+ "name": "date",
117
+ "required": false,
118
+ "type": "string",
119
+ "description": "Optional summary date in ISO format"
120
+ }
121
+ ]
122
+ }
123
+ },
124
+ "transactions": {
125
+ "getCompanyTransactions": {
126
+ "method": "GET",
127
+ "path": "/transactions/companies/:id",
128
+ "summary": "Get transactions for one company",
129
+ "description": null,
130
+ "exampleCall": "client.transactions.getCompanyTransactions('123', { companyGroupIds: [123] })",
131
+ "responseType": "TransactionsGetCompanyTransactionsResponse",
132
+ "pathParams": [
133
+ {
134
+ "name": "id",
135
+ "type": "string",
136
+ "description": "Company identifier"
137
+ }
138
+ ],
139
+ "queryParams": [
140
+ {
141
+ "name": "companyGroupIds",
142
+ "required": false,
143
+ "type": "number[]",
144
+ "description": "Optional company group identifiers to filter transactions by"
145
+ }
146
+ ]
147
+ },
148
+ "getTransactions": {
149
+ "method": "GET",
150
+ "path": "/transactions",
151
+ "summary": "Get transactions for multiple companies",
152
+ "description": null,
153
+ "exampleCall": "client.transactions.getTransactions({ companyGroupIds: [123] })",
154
+ "responseType": "TransactionsGetTransactionsResponse",
155
+ "pathParams": [],
156
+ "queryParams": [
157
+ {
158
+ "name": "companyGroupIds",
159
+ "required": false,
160
+ "type": "number[]",
161
+ "description": "Optional company group identifiers to filter transactions by"
162
+ },
163
+ {
164
+ "name": "companyIds",
165
+ "required": false,
166
+ "type": "number[]",
167
+ "description": "Optional company identifiers to filter transactions by"
168
+ }
169
+ ]
170
+ }
171
+ }
172
+ }
173
+
174
+ export class RunditSdkError extends Error {
175
+ constructor(message, details) {
176
+ super(message)
177
+ this.name = 'RunditSdkError'
178
+ this.status = details.status
179
+ this.body = details.body
180
+ }
181
+ }
182
+
183
+ function resolveFetch(customFetch) {
184
+ if (customFetch) {
185
+ return customFetch
186
+ }
187
+
188
+ if (typeof globalThis.fetch === 'function') {
189
+ return globalThis.fetch.bind(globalThis)
190
+ }
191
+
192
+ throw new Error('No fetch implementation available. Pass options.fetch when initializing the client.')
193
+ }
194
+
195
+ function normalizeBaseUrl(baseUrl) {
196
+ return baseUrl.replace(/\/+$/, '')
197
+ }
198
+
199
+ function interpolatePath(pathTemplate, pathParams = {}) {
200
+ return pathTemplate.replace(/:([A-Za-z0-9_]+)/g, (_, key) => {
201
+ const value = pathParams[key]
202
+
203
+ if (value === undefined || value === null) {
204
+ throw new Error(`Missing required path parameter: ${key}`)
205
+ }
206
+
207
+ return encodeURIComponent(String(value))
208
+ })
209
+ }
210
+
211
+ function appendQueryString(url, query = {}) {
212
+ const searchParams = new URLSearchParams()
213
+
214
+ Object.entries(query).forEach(([key, value]) => {
215
+ if (value === undefined || value === null) {
216
+ return
217
+ }
218
+
219
+ if (Array.isArray(value)) {
220
+ value.forEach((item) => {
221
+ if (item !== undefined && item !== null) {
222
+ searchParams.append(key, String(item))
223
+ }
224
+ })
225
+ return
226
+ }
227
+
228
+ searchParams.append(key, String(value))
229
+ })
230
+
231
+ const queryString = searchParams.toString()
232
+ return queryString.length > 0 ? `${url}?${queryString}` : url
233
+ }
234
+
235
+ async function parseResponse(response) {
236
+ if (response.status === 204) {
237
+ return undefined
238
+ }
239
+
240
+ const contentType = response.headers.get('content-type') || ''
241
+
242
+ if (contentType.includes('application/json')) {
243
+ return response.json()
244
+ }
245
+
246
+ return response.text()
247
+ }
248
+
249
+ function createRequester(options) {
250
+ const fetchImplementation = resolveFetch(options.fetch)
251
+ const baseUrl = normalizeBaseUrl(options.baseUrl)
252
+ const defaultHeaders = {
253
+ Accept: 'application/json',
254
+ ...options.headers,
255
+ "Authorization": `Bearer ${options.token}`,
256
+ }
257
+
258
+ return async ({ method, path, pathParams, query, signal }) => {
259
+ const url = appendQueryString(baseUrl + interpolatePath(path, pathParams), query)
260
+ const response = await fetchImplementation(url, {
261
+ method,
262
+ headers: defaultHeaders,
263
+ signal,
264
+ })
265
+
266
+ const body = await parseResponse(response)
267
+
268
+ if (!response.ok) {
269
+ throw new RunditSdkError(`Rundit request failed with status ${response.status}`, {
270
+ status: response.status,
271
+ body,
272
+ })
273
+ }
274
+
275
+ return body
276
+ }
277
+ }
278
+
279
+ function createCompaniesNamespace(request) {
280
+ return {
281
+ getOne: (id, init = {}) => request({ method: "GET", path: "/companies/:id", pathParams: { id }, query: undefined, signal: init.signal }),
282
+ getAll: (init = {}) => request({ method: "GET", path: "/companies", pathParams: undefined, query: undefined, signal: init.signal }),
283
+ }
284
+ }
285
+
286
+ function createCompanyGroupsNamespace(request) {
287
+ return {
288
+ getAll: (init = {}) => request({ method: "GET", path: "/company-groups", pathParams: undefined, query: undefined, signal: init.signal }),
289
+ getOne: (id, init = {}) => request({ method: "GET", path: "/company-groups/:id", pathParams: { id }, query: undefined, signal: init.signal }),
290
+ }
291
+ }
292
+
293
+ function createPositionsNamespace(request) {
294
+ return {
295
+ getCompanyPositions: (id, query = {}, init = {}) => request({ method: "GET", path: "/positions/companies/:id", pathParams: { id }, query: query, signal: init.signal }),
296
+ getPortfolioPositions: (query = {}, init = {}) => request({ method: "GET", path: "/positions/portfolio", pathParams: undefined, query: query, signal: init.signal }),
297
+ }
298
+ }
299
+
300
+ function createTransactionsNamespace(request) {
301
+ return {
302
+ getCompanyTransactions: (id, query = {}, init = {}) => request({ method: "GET", path: "/transactions/companies/:id", pathParams: { id }, query: query, signal: init.signal }),
303
+ getTransactions: (query = {}, init = {}) => request({ method: "GET", path: "/transactions", pathParams: undefined, query: query, signal: init.signal }),
304
+ }
305
+ }
306
+
307
+ export function createEmbedClient(options) {
308
+ if (!options || typeof options.baseUrl !== 'string' || options.baseUrl.length === 0) {
309
+ throw new Error('options.baseUrl is required')
310
+ }
311
+
312
+ if (!options.token) {
313
+ throw new Error('options.token is required')
314
+ }
315
+
316
+ const request = createRequester(options)
317
+
318
+ return {
319
+ companies: createCompaniesNamespace(request),
320
+ companyGroups: createCompanyGroupsNamespace(request),
321
+ positions: createPositionsNamespace(request),
322
+ transactions: createTransactionsNamespace(request),
323
+ }
324
+ }
325
+
326
+ export { routeManifest }