contentful / contentful-sdk-core

Core modules for the Contentful JS SDKs
MIT License
22 stars 20 forks source link

fix: Revert "feat: replace qs node API with URLSearchParams web api" #485

Closed t-col closed 4 months ago

t-col commented 4 months ago

This reverts commit 8020f4e389f85a86ce27d24b806148bf43f68342.

URLSearchParams does not stringify nested objects

wojtekmaj commented 3 months ago

Maybe we could give this another go? It shouldn't be too complex to build something of our own. For example:

function stringify(obj) {
    function stringifyInner(obj, parentKey) {
        return Object.entries(obj).flatMap(([key, value]) => {
            const fullKey = `${parentKey}[${key}]`;
            if (typeof value === 'object') return stringifyInner(value, fullKey)
            return [[fullKey, value]]
        })
    }

    return new URLSearchParams(Object.entries(obj).flatMap(([k, v]) => {
        if (typeof v === 'object') return stringifyInner(v, k)
        return [[k, v]]
    }));
}