i18next / i18next-http-backend

i18next-http-backend is a backend layer for i18next using in Node.js, in the browser and for Deno.
MIT License
443 stars 67 forks source link

parseLoadPayload for POST request #110

Closed nycruslan closed 1 year ago

nycruslan commented 1 year ago

Adding parseLoadPayload for POST request.

For everybody working with GraphQL or who would like to send a POST request - the parseLoadPayload function will be handy. It gets access to languages and namespaces and can parse the data before it has been sent by loadPath.

Checklist

Checklist (for documentation change)

adrai commented 1 year ago

Why not using the existing request option?

nycruslan commented 1 year ago

That's a good question.

When working with GraphQL would be convenient to have access to the namespaces and languages for generating a correct GraphQL POST request.

Here is a simple use case for this:

{
    loadPath: url.toString(),

    parseLoadPayload([locale]: string[], [ns]: string[]) {

      const {  variables } = graphqlVariables(ns);
      const query = queries[ns];

      const graphqlQuery = {
        query,
        variables: {
          locale,
          ...variables,
        },
      };

      return graphqlQuery;
    },

    parse(resp: string) {
      const { data } = JSON.parse(resp);
      return data;
    },
}

I considered updating the request function to access namespaces and languages, but it will require significant updates and may cause breaking changes. Same with parsePayload.

nycruslan commented 1 year ago

The request function definitely works when using additional HTTP clients like Apollo Client or Axios to make a POST request.

But since requestOptions is making a POST request, this update could be a nice addition.

phairow commented 1 year ago

Access to namespace and language would allow loading content using post payload rather than url path or using query parameters.

adrai commented 1 year ago

included in v2.2.0 thank you for your contribution