orbitjs / orbit

Composable data framework for ambitious web applications.
https://orbitjs.com
MIT License
2.33k stars 134 forks source link

serializers customization #963

Closed ahoyahoy closed 2 years ago

ahoyahoy commented 2 years ago

Please add an example of changing json:api url serializers. In particular, I want to change the model name in the url where the letter 's' is appended to the end. 🙏

xulww commented 2 years ago

You can provide custom serialization behavior by overriding the default serializer class or specifying additional serializer settings. You can read more about it in the docs.

In your case, you can add the serializerSettingsFor option to the remote source, which will let you pluralize/singularize model names. Here is an example from the docs:

const source = new JSONAPISource({
  schema,
  serializerSettingsFor: buildSerializerSettingsFor({
    sharedSettings: {
      // Optional: Custom `pluralize` / `singularize` inflectors that know about
      // your app's unique data.
      inflectors: {
        pluralize: buildInflector(
          { person: 'people' }, // custom mappings
          (input) => `${input}s` // naive pluralizer, specified as a fallback
        ),
        singularize: buildInflector(
          { people: 'person' }, // custom mappings
          (arg) => arg.substring(0, arg.length - 1) // naive singularizer, specified as a fallback
        )
      }
    },
...

Hope it helps