Skip to content

Commit 81fae4f

Browse files
authored
fix: status and cid are strings in the query result (#21)
The types expect `status` and `cid` to be arrays of strings, but they arrive as strings so the code fails. Instead of using multiple query args we use a single are with multiple values delimited by commas. Problem is this seems to be broken in oas-tools at the moment: oas-tools/oas-tools#248 This PR works around the breakage.
1 parent ad4b333 commit 81fae4f

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

service/pins.js

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const { ok, ok202, notFound } = require("./util")
99
* @returns {State}
1010
*/
1111
const init = ({
12-
accessToken = null,
12+
accessToken = null,
1313
delegates = [
1414
"/ip4/203.0.113.42/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
1515
"/ip6/2001:db8::42/tcp/8080/p2p/QmYVEDcquBLjoMEz6qxTSm5AfQ3uUcvHdxC8VUJs6sB1oh",
@@ -184,14 +184,9 @@ exports.removePin = removePin
184184
* @returns {boolean}
185185
*/
186186
const match = (query, { pin, status, created }) => {
187-
// Workarounds https://github.com/ipfs/go-ipfs/issues/7827
188-
const statuses =
189-
/** @type {string[]} */([]).concat(...(query.status || []).map(($) => $.split(",")))
190-
191-
const cids = query.cid
192-
? /** @type {string[]} */([]).concat(...query.cid.map(($) => $.split(",")))
193-
: []
194-
187+
// Workarounds https://github.com/oas-tools/oas-tools/issues/248
188+
const statuses = parseStringArr(query.status)
189+
const cids = parseStringArr(query.cid)
195190

196191
const matched =
197192
(query.cid == null || cids.includes(pin.cid)) &&
@@ -239,17 +234,33 @@ const deriveStatus = ({ name = "" }) => {
239234

240235
/**
241236
* Parse the input using Date.parse, with special casing if the input is already a Date.
242-
*
237+
*
243238
* @param {Date|string} d - a Date object or ISO-formatted date string.
244239
* @returns {number|Date} - the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC, or a Date instance
245240
*/
246241
const parseDate = (d) => {
247-
if (d instanceof Date) {
242+
if (d instanceof Date) {
248243
return d
249244
}
250245
return Date.parse(d)
251246
}
252247

248+
/**
249+
* @param {string | string[]} [s]
250+
* @returns {string[]}
251+
*/
252+
const parseStringArr = (s) => {
253+
if (s == null) {
254+
return []
255+
}
256+
257+
if (typeof s === 'string') {
258+
return s.split(',')
259+
}
260+
261+
return s
262+
}
263+
253264
/**
254265
* @typedef {Object} State
255266
* @property {string|null} accessToken

0 commit comments

Comments
 (0)