Code Monkey home page Code Monkey logo

Comments (13)

cheryl-c-tirerack avatar cheryl-c-tirerack commented on June 9, 2024 1

Thank you for your help, I simplified my custom-instance and used interceptors properly and it is solved, so I appreciate the exchange.

from orval.

melloware avatar melloware commented on June 9, 2024

I just tried it with 6.20.0 and can't reproduce. Headers False does not create headers but headers:true does. Please provide and exact reproducer.

from orval.

cheryl-c-tirerack avatar cheryl-c-tirerack commented on June 9, 2024

It is in propriety code and a large api. Can you share your config to produce no headers? Is mine in the right place?
I will try to spin it up connecting to placeholder api.

from orval.

melloware avatar melloware commented on June 9, 2024

Sure it looks like this..

module.exports = {
	myservice: {
		output: {
			target: "src/service/MyService.ts",
			client: "react-query",
			mock: false,
			prettier: false,
			headers: true,
			override: {
				useDates: false,
				mutator: {
					path: "src/service/AxiosMutator.ts",
					name: "useAxiosMutator",
				},
				query: {
					useQuery: true,
				},
			},
		},
		input: {
			target: "./openapi.json",
		},
	},
};

And my JSON looks like this for OpenAPI.

"paths": {
		"/domino/dataset/file/content/{snapshotId}": {
			"get": {
				"tags": ["Domino"],
				"summary": "Get a single file content",
				"description": "Gets the contents of a single file as a string.",
				"parameters": [
					{
						"name": "X-Domino-Api-Key",
						"in": "header",
						"description": "Domino API Key",
						"required": true,
						"schema": {
							"type": "string"
						},
						"example": "83b8568486a99e4a61a49583fd8bea78cbe51ccfbabed01fa04bedf9c875ff96"
					},
					{
						"name": "X-Domino-Api-Url",
						"in": "header",
						"description": "Domino URL",
						"required": true,
						"schema": {
							"type": "string"
						}
					},
					{
						"name": "snapshotId",
						"in": "path",
						"description": "Snapshot Identifier",
						"required": true,
						"schema": {
							"type": "string"
						}
					},
					{
						"name": "filePath",
						"in": "query",
						"description": "File Path",
						"required": true,
						"schema": {
							"type": "string"
						}
					}
				],
				"responses": {
					"200": {
						"description": "File Content",
						"content": {
							"application/json": {
								"schema": {
									"type": "string"
								}
							}
						}
					},
					"403": {
						"description": "Forbidden",
						"content": {
							"application/json": {
								"schema": {
									"$ref": "#/components/schemas/ApiError"
								}
							}
						}
					},
					"500": {
						"description": "Unexpected error",
						"content": {
							"application/json": {
								"schema": {
									"$ref": "#/components/schemas/ApiError"
								}
							}
						}
					}
				}
			}
		},

from orval.

cheryl-c-tirerack avatar cheryl-c-tirerack commented on June 9, 2024

Thank you for sharing config-just wanted to make sure I had mine in the right place.
Here is a minimum repo
https://github.com/cheryl-c-tirerack/orval-debug

See
api/partners/post.ts
The createPartner is giving a header that conflicts with other type definitions. My backend says that is not coming from the json, shown in repo above.
Thanks for your time.

from orval.

melloware avatar melloware commented on June 9, 2024

Ohhh yours are Response output headers not Request input headers. That is what that property currently controls. Let me look.

from orval.

melloware avatar melloware commented on June 9, 2024

Wait these are the Content-Type headers to tell the server you want application-json? Why do you want those excluded?

It should always generate headers: { "Content-Type": "application/json" } because your openAPI says

"responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Partner"
                }
              }
            }
          },
        }

from orval.

cheryl-c-tirerack avatar cheryl-c-tirerack commented on June 9, 2024

I have headers set in my custom-instance, not shown in the reproduction.
If headers are set in my custom-instance has several, but the generated code does not show all of them, I get an eslint fail on build. It would be easier if all headers were controlled by my axios settings.

from orval.

melloware avatar melloware commented on June 9, 2024

Then I need a better example because that example above is working as expected? And can't you just override the headers with an Axios Mutator?

from orval.

cheryl-c-tirerack avatar cheryl-c-tirerack commented on June 9, 2024

I've updated the example with my full custom-instance.
The headers here do not match the headers in the generated code and so I get eslint / typescript errors on build.
I have struggled a bit to understand this version of the axios instance vs interceptors and other methods I see online. So this could be a failure of understanding on my part. Everything worked perfectly until I hit an api endpoint that generates those for me.

from orval.

melloware avatar melloware commented on June 9, 2024

oh I have always used Interceptors to inject headers.

import { AxiosInstance } from "axios";

/**
 * Axios utlity class for adding token handling and Date handling to all request/responses.
 */
export default class AxiosInterceptors {
	// hold instances of interceptor by their unique URL
	static requestInterceptors = new Map<string, number>();
	static responseInterceptors = new Map<string, number>();

	/**
	 * Configures Axios request/reponse interceptors to add JWT token.
	 *
	 * @param {AxiosInstance} instance the Axios instance to remove interceptors
	 * @param {string} apiKey the API Key to add to all Axios requests
	 * @param {string} apiUrl the API URL to add to all Axios requests
	 */
	static setupAxiosInstance = (instance: AxiosInstance, apiKey: string, apiUrl: string) => {
		const appKey = instance.defaults.baseURL!;
		EnvUtils.debug(`Configuring Axios request/response interceptors for: ${apiKey}`);
		// Axios request interceptor to add JWT token
		const tokenRequestInterceptor = instance.interceptors.request.use(
			(config) => {
				if (apiKey) {
					const headers = config.headers || {};
					headers["X-Domino-Api-Key"] = apiKey;
					headers["X-Domino-Api-Url"] = apiUrl;
				}
				return config;
			},
			(error) => {
				return Promise.reject(error);
			},
		);
		EnvUtils.debug(`Axios Token Request Interceptor: ${tokenRequestInterceptor}`);
		AxiosInterceptors.requestInterceptors.set(appKey, tokenRequestInterceptor);
	};

	/**
	 * Cleanup Axios on sign out of application.
	 *
	 * @param {AxiosInstance} instance the Axios instance to remove interceptors
	 */
	static teardownAxiosInstance = (instance: AxiosInstance) => {
		const appKey = instance.defaults.baseURL!;
		EnvUtils.warn(`Cleaning up Axios removing all interceptors for: ${appKey}`);
		instance.interceptors.request.clear();
		instance.interceptors.response.clear();
	};
}

Then use it like this setting it up once.

AxiosInterceptors.setupAxiosInstance(AXIOS_INSTANCE, dominoApiKey, dominoApiInstance);

from orval.

melloware avatar melloware commented on June 9, 2024

Going to close this for now as not an Orval issue

from orval.

melloware avatar melloware commented on June 9, 2024

No problem!

from orval.

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.