Code Monkey home page Code Monkey logo

Comments (4)

acerola1 avatar acerola1 commented on May 28, 2024 1

Yes it is not easy. Maybe it should be more finer grained. Array and object serialization can be different. I created a serializer for formExplode for object. It may help someone.

const serializePrimitive = (key, value) => `${key}=${encodeURIComponent(String(value))}`;

const serializeArray = (key, value, serializer) => {
  if (!value.length) return undefined;
  return value
    .map((item) => serializer(key, item))
    .filter(Boolean)
    .join('&');
};

const serializeObject = (value, serializer) => {
  const entries = Object.entries(value).filter(([_, v]) => v !== undefined && v !== null);
  if (!entries.length) return undefined;
  return entries
    .map(([k, v]) => serializer(k, v))
    .filter(Boolean)
    .join('&');
};

const queryParamSerializer = (key, value) => {
  if (value === null || value === undefined) return undefined;

  const type = typeof value;
  if (type === 'string' || type === 'number' || type === 'boolean') return serializePrimitive(key, value);
  if (Array.isArray(value)) return serializeArray(key, value, queryParamSerializer);
  if (type === 'object') return serializeObject(value, queryParamSerializer);

  return undefined;
};

export const querySerializer = (q) => {
  const search: string[] = [];
  if (q && typeof q === 'object') {
    for (const [k, v] of Object.entries(q)) {
      const value = queryParamSerializer(k, v);
      if (value) {
        search.push(value);
      }
    }
  }
  return search.join('&');
};

from openapi-typescript.

drwpow avatar drwpow commented on May 28, 2024

You are right the docs are incorrect and says the default is form/explode when it’s deepObject. The docs should be fixed, but I’m unsure if the default should be changed again, because there were many issues opened about simpler serializers being used.

No matter what the default is, there will always be a large number of users whose backends implement a different style. In that vein, would it be worthwhile to ship some additional querySerializers that handle alternate syntaxes? Perhaps something like:

import createClient from 'openapi-fetch';
import { formExplode } from 'openapi/serializers';

createClient({
  querySerializer: formExplode
});

from openapi-typescript.

drwpow avatar drwpow commented on May 28, 2024

I really like the idea of separating object + array serialization! I don’t think anyone’s suggested that before. Adding that would make it easier for people to customize their own serializers for sure, and that could be added in a backwards-compatible way.

from openapi-typescript.

drwpow avatar drwpow commented on May 28, 2024

Also digging into this, I realize that we don’t support path serialization very well either, but should. Will add a minor breaking change to the library to handle this (and provide for easier overrides).

Since this is a breaking change, this will probably be released in 0.9.0 along with #1521.

from openapi-typescript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.