@tidal-music/api 0.2.0 → 0.3.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 (4) hide show
  1. package/README.md +1 -0
  2. package/dist/index.d.ts +1717 -1649
  3. package/dist/index.js +180 -161
  4. package/package.json +10 -10
package/dist/index.js CHANGED
@@ -1,148 +1,167 @@
1
- const B = /\{[^{}]+\}/g, F = () => {
1
+ const M = /\{[^{}]+\}/g, W = () => {
2
2
  var r, t;
3
3
  return typeof process == "object" && Number.parseInt((t = (r = process == null ? void 0 : process.versions) == null ? void 0 : r.node) == null ? void 0 : t.substring(0, 2)) >= 18 && process.versions.undici;
4
4
  };
5
- function M() {
5
+ function Q() {
6
6
  return Math.random().toString(36).slice(2, 11);
7
7
  }
8
- function W(r) {
8
+ function G(r) {
9
9
  let {
10
10
  baseUrl: t = "",
11
11
  Request: e = globalThis.Request,
12
- fetch: i = globalThis.fetch,
12
+ fetch: o = globalThis.fetch,
13
13
  querySerializer: n,
14
- bodySerializer: o,
14
+ bodySerializer: i,
15
15
  headers: l,
16
- requestInitExt: f = void 0,
17
- ...v
16
+ requestInitExt: c = void 0,
17
+ ...B
18
18
  } = { ...r };
19
- f = F() ? f : void 0, t = H(t);
19
+ c = W() ? c : void 0, t = P(t);
20
20
  const y = [];
21
21
  async function h(a, s) {
22
22
  const {
23
- baseUrl: m,
24
- fetch: C = i,
25
- Request: K = e,
26
- headers: P,
23
+ baseUrl: p,
24
+ fetch: z = o,
25
+ Request: V = e,
26
+ headers: q,
27
27
  params: w = {},
28
- parseAs: S = "json",
29
- querySerializer: j,
30
- bodySerializer: U = o ?? G,
31
- body: O,
32
- ...q
28
+ parseAs: A = "json",
29
+ querySerializer: g,
30
+ bodySerializer: O = i ?? _,
31
+ body: I,
32
+ ...$
33
33
  } = s || {};
34
- m && (t = H(m));
35
- let z = typeof n == "function" ? n : I(n);
36
- j && (z = typeof j == "function" ? j : I({
34
+ let C = t;
35
+ p && (C = P(p) ?? t);
36
+ let v = typeof n == "function" ? n : H(n);
37
+ g && (v = typeof g == "function" ? g : H({
37
38
  ...typeof n == "object" ? n : {},
38
- ...j
39
+ ...g
39
40
  }));
40
- const x = O === void 0 ? void 0 : U(O), L = (
41
+ const U = I === void 0 ? void 0 : O(
42
+ I,
43
+ // Note: we declare mergeHeaders() both here and below because it’s a bit of a chicken-or-egg situation:
44
+ // bodySerializer() needs all headers so we aren’t dropping ones set by the user, however,
45
+ // the result of this ALSO sets the lowest-priority content-type header. So we re-merge below,
46
+ // setting the content-type at the very beginning to be overwritten.
47
+ // Lastly, based on the way headers work, it’s not a simple “present-or-not” check becauase null intentionally un-sets headers.
48
+ D(l, q, w.header)
49
+ ), F = D(
41
50
  // with no body, we should not to set Content-Type
42
- x === void 0 || // if serialized body is FormData; browser will correctly set Content-Type & boundary expression
43
- x instanceof FormData ? {} : {
51
+ U === void 0 || // if serialized body is FormData; browser will correctly set Content-Type & boundary expression
52
+ U instanceof FormData ? {} : {
44
53
  "Content-Type": "application/json"
45
- }
54
+ },
55
+ l,
56
+ q,
57
+ w.header
46
58
  ), N = {
47
59
  redirect: "follow",
48
- ...v,
49
- ...q,
50
- body: x,
51
- headers: _(L, l, P, w.header)
60
+ ...B,
61
+ ...$,
62
+ body: U,
63
+ headers: F
52
64
  };
53
- let g, E, b = new e(J(a, { baseUrl: t, params: w, querySerializer: z }), N);
54
- for (const u in q)
55
- u in b || (b[u] = q[u]);
65
+ let j, E, m = new e(
66
+ K(a, { baseUrl: C, params: w, querySerializer: v }),
67
+ N
68
+ ), f;
69
+ for (const d in $)
70
+ d in m || (m[d] = $[d]);
56
71
  if (y.length) {
57
- g = M(), E = Object.freeze({
58
- baseUrl: t,
59
- fetch: C,
60
- parseAs: S,
61
- querySerializer: z,
62
- bodySerializer: U
72
+ j = Q(), E = Object.freeze({
73
+ baseUrl: C,
74
+ fetch: z,
75
+ parseAs: A,
76
+ querySerializer: v,
77
+ bodySerializer: O
63
78
  });
64
- for (const u of y)
65
- if (u && typeof u == "object" && typeof u.onRequest == "function") {
66
- const d = await u.onRequest({
67
- request: b,
79
+ for (const d of y)
80
+ if (d && typeof d == "object" && typeof d.onRequest == "function") {
81
+ const u = await d.onRequest({
82
+ request: m,
68
83
  schemaPath: a,
69
84
  params: w,
70
85
  options: E,
71
- id: g
86
+ id: j
72
87
  });
73
- if (d) {
74
- if (!(d instanceof e))
75
- throw new Error("onRequest: must return new Request() when modifying the request");
76
- b = d;
77
- }
88
+ if (u)
89
+ if (u instanceof e)
90
+ m = u;
91
+ else if (u instanceof Response) {
92
+ f = u;
93
+ break;
94
+ } else
95
+ throw new Error("onRequest: must return new Request() or Response() when modifying the request");
78
96
  }
79
97
  }
80
- let c;
81
- try {
82
- c = await C(b, f);
83
- } catch (u) {
84
- let d = u;
98
+ if (!f) {
99
+ try {
100
+ f = await z(m, c);
101
+ } catch (d) {
102
+ let u = d;
103
+ if (y.length)
104
+ for (let b = y.length - 1; b >= 0; b--) {
105
+ const T = y[b];
106
+ if (T && typeof T == "object" && typeof T.onError == "function") {
107
+ const R = await T.onError({
108
+ request: m,
109
+ error: u,
110
+ schemaPath: a,
111
+ params: w,
112
+ options: E,
113
+ id: j
114
+ });
115
+ if (R) {
116
+ if (R instanceof Response) {
117
+ u = void 0, f = R;
118
+ break;
119
+ }
120
+ if (R instanceof Error) {
121
+ u = R;
122
+ continue;
123
+ }
124
+ throw new Error("onError: must return new Response() or instance of Error");
125
+ }
126
+ }
127
+ }
128
+ if (u)
129
+ throw u;
130
+ }
85
131
  if (y.length)
86
- for (let p = y.length - 1; p >= 0; p--) {
87
- const $ = y[p];
88
- if ($ && typeof $ == "object" && typeof $.onError == "function") {
89
- const R = await $.onError({
90
- request: b,
91
- error: d,
132
+ for (let d = y.length - 1; d >= 0; d--) {
133
+ const u = y[d];
134
+ if (u && typeof u == "object" && typeof u.onResponse == "function") {
135
+ const b = await u.onResponse({
136
+ request: m,
137
+ response: f,
92
138
  schemaPath: a,
93
139
  params: w,
94
140
  options: E,
95
- id: g
141
+ id: j
96
142
  });
97
- if (R) {
98
- if (R instanceof Response) {
99
- d = void 0, c = R;
100
- break;
101
- }
102
- if (R instanceof Error) {
103
- d = R;
104
- continue;
105
- }
106
- throw new Error("onError: must return new Response() or instance of Error");
143
+ if (b) {
144
+ if (!(b instanceof Response))
145
+ throw new Error("onResponse: must return new Response() when modifying the response");
146
+ f = b;
107
147
  }
108
148
  }
109
149
  }
110
- if (d)
111
- throw d;
112
150
  }
113
- if (y.length)
114
- for (let u = y.length - 1; u >= 0; u--) {
115
- const d = y[u];
116
- if (d && typeof d == "object" && typeof d.onResponse == "function") {
117
- const p = await d.onResponse({
118
- request: b,
119
- response: c,
120
- schemaPath: a,
121
- params: w,
122
- options: E,
123
- id: g
124
- });
125
- if (p) {
126
- if (!(p instanceof Response))
127
- throw new Error("onResponse: must return new Response() when modifying the response");
128
- c = p;
129
- }
130
- }
131
- }
132
- if (c.status === 204 || c.headers.get("Content-Length") === "0")
133
- return c.ok ? { data: void 0, response: c } : { error: void 0, response: c };
134
- if (c.ok)
135
- return S === "stream" ? { data: c.body, response: c } : { data: await c[S](), response: c };
136
- let T = await c.text();
151
+ if (f.status === 204 || m.method === "HEAD" || f.headers.get("Content-Length") === "0")
152
+ return f.ok ? { data: void 0, response: f } : { error: void 0, response: f };
153
+ if (f.ok)
154
+ return A === "stream" ? { data: f.body, response: f } : { data: await f[A](), response: f };
155
+ let x = await f.text();
137
156
  try {
138
- T = JSON.parse(T);
157
+ x = JSON.parse(x);
139
158
  } catch {
140
159
  }
141
- return { error: T, response: c };
160
+ return { error: x, response: f };
142
161
  }
143
162
  return {
144
- request(a, s, m) {
145
- return h(s, { ...m, method: a.toUpperCase() });
163
+ request(a, s, p) {
164
+ return h(s, { ...p, method: a.toUpperCase() });
146
165
  },
147
166
  /** Call a GET endpoint */
148
167
  GET(a, s) {
@@ -188,13 +207,13 @@ function W(r) {
188
207
  /** Unregister middleware */
189
208
  eject(...a) {
190
209
  for (const s of a) {
191
- const m = y.indexOf(s);
192
- m !== -1 && y.splice(m, 1);
210
+ const p = y.indexOf(s);
211
+ p !== -1 && y.splice(p, 1);
193
212
  }
194
213
  }
195
214
  };
196
215
  }
197
- function A(r, t, e) {
216
+ function S(r, t, e) {
198
217
  if (t == null)
199
218
  return "";
200
219
  if (typeof t == "object")
@@ -203,18 +222,18 @@ function A(r, t, e) {
203
222
  );
204
223
  return `${r}=${(e == null ? void 0 : e.allowReserved) === !0 ? t : encodeURIComponent(t)}`;
205
224
  }
206
- function D(r, t, e) {
225
+ function k(r, t, e) {
207
226
  if (!t || typeof t != "object")
208
227
  return "";
209
- const i = [], n = {
228
+ const o = [], n = {
210
229
  simple: ",",
211
230
  label: ".",
212
231
  matrix: ";"
213
232
  }[e.style] || "&";
214
233
  if (e.style !== "deepObject" && e.explode === !1) {
215
- for (const f in t)
216
- i.push(f, e.allowReserved === !0 ? t[f] : encodeURIComponent(t[f]));
217
- const l = i.join(",");
234
+ for (const c in t)
235
+ o.push(c, e.allowReserved === !0 ? t[c] : encodeURIComponent(t[c]));
236
+ const l = o.join(",");
218
237
  switch (e.style) {
219
238
  case "form":
220
239
  return `${r}=${l}`;
@@ -227,17 +246,17 @@ function D(r, t, e) {
227
246
  }
228
247
  }
229
248
  for (const l in t) {
230
- const f = e.style === "deepObject" ? `${r}[${l}]` : l;
231
- i.push(A(f, t[l], e));
249
+ const c = e.style === "deepObject" ? `${r}[${l}]` : l;
250
+ o.push(S(c, t[l], e));
232
251
  }
233
- const o = i.join(n);
234
- return e.style === "label" || e.style === "matrix" ? `${n}${o}` : o;
252
+ const i = o.join(n);
253
+ return e.style === "label" || e.style === "matrix" ? `${n}${i}` : i;
235
254
  }
236
- function k(r, t, e) {
255
+ function L(r, t, e) {
237
256
  if (!Array.isArray(t))
238
257
  return "";
239
258
  if (e.explode === !1) {
240
- const o = { form: ",", spaceDelimited: "%20", pipeDelimited: "|" }[e.style] || ",", l = (e.allowReserved === !0 ? t : t.map((f) => encodeURIComponent(f))).join(o);
259
+ const i = { form: ",", spaceDelimited: "%20", pipeDelimited: "|" }[e.style] || ",", l = (e.allowReserved === !0 ? t : t.map((c) => encodeURIComponent(c))).join(i);
241
260
  switch (e.style) {
242
261
  case "simple":
243
262
  return l;
@@ -251,23 +270,23 @@ function k(r, t, e) {
251
270
  return `${r}=${l}`;
252
271
  }
253
272
  }
254
- const i = { simple: ",", label: ".", matrix: ";" }[e.style] || "&", n = [];
255
- for (const o of t)
256
- e.style === "simple" || e.style === "label" ? n.push(e.allowReserved === !0 ? o : encodeURIComponent(o)) : n.push(A(r, o, e));
257
- return e.style === "label" || e.style === "matrix" ? `${i}${n.join(i)}` : n.join(i);
273
+ const o = { simple: ",", label: ".", matrix: ";" }[e.style] || "&", n = [];
274
+ for (const i of t)
275
+ e.style === "simple" || e.style === "label" ? n.push(e.allowReserved === !0 ? i : encodeURIComponent(i)) : n.push(S(r, i, e));
276
+ return e.style === "label" || e.style === "matrix" ? `${o}${n.join(o)}` : n.join(o);
258
277
  }
259
- function I(r) {
278
+ function H(r) {
260
279
  return function(e) {
261
- const i = [];
280
+ const o = [];
262
281
  if (e && typeof e == "object")
263
282
  for (const n in e) {
264
- const o = e[n];
265
- if (o != null) {
266
- if (Array.isArray(o)) {
267
- if (o.length === 0)
283
+ const i = e[n];
284
+ if (i != null) {
285
+ if (Array.isArray(i)) {
286
+ if (i.length === 0)
268
287
  continue;
269
- i.push(
270
- k(n, o, {
288
+ o.push(
289
+ L(n, i, {
271
290
  style: "form",
272
291
  explode: !0,
273
292
  ...r == null ? void 0 : r.array,
@@ -276,9 +295,9 @@ function I(r) {
276
295
  );
277
296
  continue;
278
297
  }
279
- if (typeof o == "object") {
280
- i.push(
281
- D(n, o, {
298
+ if (typeof i == "object") {
299
+ o.push(
300
+ k(n, i, {
282
301
  style: "deepObject",
283
302
  explode: !0,
284
303
  ...r == null ? void 0 : r.object,
@@ -287,75 +306,75 @@ function I(r) {
287
306
  );
288
307
  continue;
289
308
  }
290
- i.push(A(n, o, r));
309
+ o.push(S(n, i, r));
291
310
  }
292
311
  }
293
- return i.join("&");
312
+ return o.join("&");
294
313
  };
295
314
  }
296
- function Q(r, t) {
315
+ function J(r, t) {
297
316
  let e = r;
298
- for (const i of r.match(B) ?? []) {
299
- let n = i.substring(1, i.length - 1), o = !1, l = "simple";
300
- if (n.endsWith("*") && (o = !0, n = n.substring(0, n.length - 1)), n.startsWith(".") ? (l = "label", n = n.substring(1)) : n.startsWith(";") && (l = "matrix", n = n.substring(1)), !t || t[n] === void 0 || t[n] === null)
317
+ for (const o of r.match(M) ?? []) {
318
+ let n = o.substring(1, o.length - 1), i = !1, l = "simple";
319
+ if (n.endsWith("*") && (i = !0, n = n.substring(0, n.length - 1)), n.startsWith(".") ? (l = "label", n = n.substring(1)) : n.startsWith(";") && (l = "matrix", n = n.substring(1)), !t || t[n] === void 0 || t[n] === null)
301
320
  continue;
302
- const f = t[n];
303
- if (Array.isArray(f)) {
304
- e = e.replace(i, k(n, f, { style: l, explode: o }));
321
+ const c = t[n];
322
+ if (Array.isArray(c)) {
323
+ e = e.replace(o, L(n, c, { style: l, explode: i }));
305
324
  continue;
306
325
  }
307
- if (typeof f == "object") {
308
- e = e.replace(i, D(n, f, { style: l, explode: o }));
326
+ if (typeof c == "object") {
327
+ e = e.replace(o, k(n, c, { style: l, explode: i }));
309
328
  continue;
310
329
  }
311
330
  if (l === "matrix") {
312
- e = e.replace(i, `;${A(n, f)}`);
331
+ e = e.replace(o, `;${S(n, c)}`);
313
332
  continue;
314
333
  }
315
- e = e.replace(i, l === "label" ? `.${encodeURIComponent(f)}` : encodeURIComponent(f));
334
+ e = e.replace(o, l === "label" ? `.${encodeURIComponent(c)}` : encodeURIComponent(c));
316
335
  }
317
336
  return e;
318
337
  }
319
- function G(r) {
320
- return r instanceof FormData ? r : JSON.stringify(r);
338
+ function _(r, t) {
339
+ return r instanceof FormData ? r : t && (t.get instanceof Function ? t.get("Content-Type") ?? t.get("content-type") : t["Content-Type"] ?? t["content-type"]) === "application/x-www-form-urlencoded" ? new URLSearchParams(r).toString() : JSON.stringify(r);
321
340
  }
322
- function J(r, t) {
341
+ function K(r, t) {
323
342
  var n;
324
343
  let e = `${t.baseUrl}${r}`;
325
- (n = t.params) != null && n.path && (e = Q(e, t.params.path));
326
- let i = t.querySerializer(t.params.query ?? {});
327
- return i.startsWith("?") && (i = i.substring(1)), i && (e += `?${i}`), e;
344
+ (n = t.params) != null && n.path && (e = J(e, t.params.path));
345
+ let o = t.querySerializer(t.params.query ?? {});
346
+ return o.startsWith("?") && (o = o.substring(1)), o && (e += `?${o}`), e;
328
347
  }
329
- function _(...r) {
348
+ function D(...r) {
330
349
  const t = new Headers();
331
350
  for (const e of r) {
332
351
  if (!e || typeof e != "object")
333
352
  continue;
334
- const i = e instanceof Headers ? e.entries() : Object.entries(e);
335
- for (const [n, o] of i)
336
- if (o === null)
353
+ const o = e instanceof Headers ? e.entries() : Object.entries(e);
354
+ for (const [n, i] of o)
355
+ if (i === null)
337
356
  t.delete(n);
338
- else if (Array.isArray(o))
339
- for (const l of o)
357
+ else if (Array.isArray(i))
358
+ for (const l of i)
340
359
  t.append(n, l);
341
- else o !== void 0 && t.set(n, o);
360
+ else i !== void 0 && t.set(n, i);
342
361
  }
343
362
  return t;
344
363
  }
345
- function H(r) {
364
+ function P(r) {
346
365
  return r.endsWith("/") ? r.substring(0, r.length - 1) : r;
347
366
  }
348
- function V(r) {
367
+ function X(r) {
349
368
  const t = {
350
- async onRequest({ request: i }) {
369
+ async onRequest({ request: o }) {
351
370
  const n = await r.getCredentials();
352
- return i.headers.set("Authorization", `Bearer ${n.token}`), i;
371
+ return o.headers.set("Authorization", `Bearer ${n.token}`), (o.method === "POST" || o.method === "PATCH" || o.method === "DELETE") && o.headers.set("Content-Type", "application/vnd.api+json"), o;
353
372
  }
354
- }, e = W({
373
+ }, e = G({
355
374
  baseUrl: "https://openapi.tidal.com/v2/"
356
375
  });
357
376
  return e.use(t), e;
358
377
  }
359
378
  export {
360
- V as createAPIClient
379
+ X as createAPIClient
361
380
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tidal-music/api",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"
@@ -17,17 +17,17 @@
17
17
  "default": "./dist/index.js"
18
18
  },
19
19
  "dependencies": {
20
- "openapi-fetch": "0.13.4",
21
- "@tidal-music/api": "0.2.0"
20
+ "openapi-fetch": "0.14.0",
21
+ "@tidal-music/api": "0.3.0"
22
22
  },
23
23
  "devDependencies": {
24
- "@vitest/coverage-v8": "3.0.8",
25
- "@vitest/ui": "3.0.8",
26
- "openapi-typescript": "7.6.1",
27
- "typescript": "5.8.2",
28
- "vite": "6.2.1",
29
- "vite-plugin-dts": "4.5.3",
30
- "vitest": "3.0.8",
24
+ "@vitest/coverage-v8": "3.2.3",
25
+ "@vitest/ui": "3.2.3",
26
+ "openapi-typescript": "7.8.0",
27
+ "typescript": "5.8.3",
28
+ "vite": "6.3.5",
29
+ "vite-plugin-dts": "4.5.4",
30
+ "vitest": "3.2.3",
31
31
  "@tidal-music/auth": "^1.3.4",
32
32
  "@tidal-music/common": "^0.1.5"
33
33
  },