Comments (5)
That should be plenty, thank you!
from openapi-ts.
Thanks @wizzyto12! Are you able to share the spec and expected results where it fails? Might be easier for me to fix content types that way
from openapi-ts.
Hey!
So this is my interceptors
OpenAPI.interceptors.request.use(async (request) => {
request.headers = {
...request.headers,
Accept: "application/ld+json",
"Content-Type":
request.method === "PATCH"
? "application/merge-patch+json"
: "application/ld+json",
};
// Here I suppose that this can probably set the token without me adding it to the header manually. I also tried setting it as a string.
// OpenAPI.TOKEN = async () => {
// const session = await auth();
//
// return session.user.access_token;
// };
const session = await auth();
// This is what works, but its obvious it will.
if (session?.user?.access_token) {
request.headers = {
...request.headers,
Authorization: Bearer ${session.user.access_token},
};
}
return request;
});
This is an entry from my openapi json file, which is openapi v3.1
"\/api\/booking_sessions": {
"post": {
"operationId": "api_booking_sessions_post",
"tags": [
"BookingSession"
],
"responses": {
"201": {
"description": "BookingSession resource created",
"content": {
"application\/ld+json": {
"schema": {
"$ref": "#\/components\/schemas\/BookingSession.jsonld-thing.get"
}
}
},
"links": {}
},
"400": {
"description": "Invalid input"
},
"422": {
"description": "Unprocessable entity"
}
},
"summary": "Creates a BookingSession resource.",
"description": "Creates a BookingSession resource.",
"parameters": [],
"requestBody": {
"description": "The new BookingSession resource",
"content": {
"application\/ld+json": {
"schema": {
"$ref": "#\/components\/schemas\/BookingSession.jsonld-booking-session.post_thing.post"
}
}
},
"required": true
},
"deprecated": false
},
"parameters": []
},
We can see that it has the appropriate "application/ld+json", which is not respected, or I don't know how to make it work.
Usage in my code, random example
async function login(credentials: { email: string; password: string }) {
const response = await LoginCheckService.appAuthPost({
requestBody: credentials,
});
if (response.token) {
const decodedToken = jwtDecode(response.token) as DecodedToken;
return { .... } as User;
}
return null;
}
I get a 406 from my API if the content is not set in the interceptors accordingly.
If thats not clear enough (I know its all around the place with the codes, but still), I can create an MVP example of api platform and nextjs to try to recreate the issue in a clean project.
Thanks for the fast reply!
from openapi-ts.
@mrlubos In case this is helpful, I have debugged it and turn out the reason the header is not attached is because the header constructor, it don't get the object passed in. As the code below, the first console.log (label headers 164
) have enough headers property (including content-type), but the second log is empty.
For now, I will use interceptor and set content-type manually to work around it.
export const getHeaders = async (
config: OpenAPIConfig,
options: ApiRequestOptions
): Promise<Headers> => {
const [token, username, password, additionalHeaders] = await Promise.all([
resolve(options, config.TOKEN),
resolve(options, config.USERNAME),
resolve(options, config.PASSWORD),
resolve(options, config.HEADERS),
]);
const headers = Object.entries({
Accept: 'application/json',
...additionalHeaders,
...options.headers,
})
.filter(([, value]) => value !== undefined && value !== null)
.reduce(
(headers, [key, value]) => ({
...headers,
[key]: String(value),
}),
{} as Record<string, string>
);
if (isStringWithValue(token)) {
headers['Authorization'] = `Bearer ${token}`;
}
if (isStringWithValue(username) && isStringWithValue(password)) {
const credentials = base64(`${username}:${password}`);
headers['Authorization'] = `Basic ${credentials}`;
}
if (options.body !== undefined) {
if (options.mediaType) {
headers['Content-Type'] = options.mediaType;
} else if (isBlob(options.body)) {
headers['Content-Type'] = options.body.type || 'application/octet-stream';
} else if (isString(options.body)) {
headers['Content-Type'] = 'text/plain';
} else if (!isFormData(options.body)) {
headers['Content-Type'] = 'application/json';
}
}
console.log('headers 164', headers)
console.log('new Headers(headers)', new Headers(headers))
return new Headers(headers);
};
from openapi-ts.
Hey, I am going to close this issue as it pertains to legacy clients. These are now in maintenance mode and I am open to pull requests, so if anyone sees this and would like to fix it, please open a pull request. The recommended approach moving forward is to use standalone clients that do not suffer from this type of problems.
from openapi-ts.
Related Issues (20)
- Help catch non declared query or path parameters HOT 10
- Interceptor Duplicates When Modifying and Saving _layout.tsx File in Expo Project Using client-fetch
- Add `clear()` method to remove all interceptors
- Axios gets imported wrong during testing HOT 5
- V0.53.0 generates empty core/request.ts HOT 4
- Option to ignore headers HOT 11
- Colon in path results in invalid generation HOT 2
- Using wrong client config parameter leads to undefined HttpRequestConstructor HOT 3
- BaseURL not being set when using client.setConfig HOT 1
- Tanstack Query Incorrect onError Types HOT 4
- Improved types for Vue Tanstack Query HOT 1
- Concurrent createClient calls resolve to same input HOT 1
- fetch-api mistakenly serializes FormData as application/json instead of multipart/form-data
- Transform nullable Dates
- react-query `createQueryKey` uses `baseUrl` of global client rather than `options.client` HOT 1
- TanStack Query (Vue) code generates `body` parameter for infinite list
- Generating services incorrectly HOT 5
- Option to customize schema names and/or $ prefix HOT 3
- Discriminator in OpenApi schema not recognized correctly HOT 2
- Available Clients add Nuxt.js support HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from openapi-ts.