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