@panomc/client 1.1.0-dev.1 → 1.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@panomc/client",
3
- "version": "1.1.0-dev.1",
3
+ "version": "1.1.0",
4
4
  "description": "Typed JavaScript client for the Pano HTTP API (JSDoc types, no dependencies, no framework).",
5
5
  "author": "PanoMC",
6
6
  "license": "MIT",
package/src/client.js CHANGED
@@ -23,12 +23,24 @@ function isRawBody(body) {
23
23
  )
24
24
  }
25
25
 
26
- /** @param {string} path @param {Record<string, any>|undefined} values */
26
+ /**
27
+ * The operation's path with its `{name}` parameters filled in, or the first parameter whose value is missing, empty,
28
+ * `.` / `..`: those would leave the literal `{name}` in the URL or move the request to another endpoint.
29
+ * @param {string} path @param {Record<string, any>|undefined} values
30
+ * @returns {{ path: string } | { missing: string }}
31
+ */
27
32
  function fillPath(path, values) {
28
- return path.replace(/\{([^}]+)\}/g, (whole, name) => {
33
+ let missing = null
34
+ const filled = path.replace(/\{([^}]+)\}/g, (whole, name) => {
29
35
  const value = values?.[name]
30
- return value === undefined || value === null ? whole : encodeURIComponent(String(value))
36
+ const text = value === undefined || value === null ? '' : String(value)
37
+ if (text === '' || text === '.' || text === '..') {
38
+ missing ??= name
39
+ return whole
40
+ }
41
+ return encodeURIComponent(text)
31
42
  })
43
+ return missing === null ? { path: filled } : { missing }
32
44
  }
33
45
 
34
46
  /** @param {Record<string, any>|undefined} query */
@@ -196,7 +208,13 @@ export function createClient(options) {
196
208
  async function request(operation, params = {}) {
197
209
  try {
198
210
  const method = String(operation.method).toUpperCase()
199
- const path = fillPath(operation.path, params.path)
211
+ const filled = fillPath(operation.path, params.path)
212
+ if ('missing' in filled) {
213
+ return failure(0, 'INVALID_PATH_PARAM', {
214
+ message: `Path parameter "${filled.missing}" is missing, empty, "." or ".."`
215
+ })
216
+ }
217
+ const path = filled.path
200
218
  const search = queryString(params.query)
201
219
  const mutation = !SAFE_METHODS.has(method)
202
220
  const useCsrf = csrfMode === 'auto' && mutation && path !== CSRF_PATH
@@ -42,6 +42,22 @@ describe('request building', () => {
42
42
  expect(f.calls[0].headers['Accept-Language']).toBe('tr')
43
43
  })
44
44
 
45
+ test("a path value that is missing, empty, '.' or '..' fails before any request", async () => {
46
+ const f = fakeFetch()
47
+ const c = createClient({ baseUrl: base, fetch: f, csrf: 'off' })
48
+ const op = { method: 'GET', path: '/api/v1/posts/{slug}/comments' }
49
+ for (const slug of [undefined, null, '', '.', '..']) {
50
+ const r = await c.request(op, { path: { slug } })
51
+ expect(r.ok).toBe(false)
52
+ expect(r.error.code).toBe('INVALID_PATH_PARAM')
53
+ }
54
+ expect(f.calls.length).toBe(0)
55
+
56
+ const ok = fakeFetch({ 'GET /api/v1/posts/..a/comments': json(200, {}) })
57
+ await createClient({ baseUrl: base, fetch: ok, csrf: 'off' }).request(op, { path: { slug: '..a' } })
58
+ expect(ok.calls[0].url).toBe(`${base}/api/v1/posts/..a/comments`)
59
+ })
60
+
45
61
  test('proxy prefix as baseUrl', async () => {
46
62
  const f = fakeFetch()
47
63
  await createClient({ baseUrl: '/pano', fetch: f, csrf: 'off' }).request({ method: 'GET', path: '/api/v1/posts' })