Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Jul 19, 2023
1 parent 444a639 commit aaf3fd8
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 94 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-dolphins-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'fets': patch
---

Fix Authentication support with JSON bodies
189 changes: 95 additions & 94 deletions packages/fets/src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ export type OASRequestParams<
TOAS extends OpenAPIDocument,
TPath extends keyof OASPathMap<TOAS>,
TMethod extends keyof OASMethodMap<TOAS, TPath>,
> = OASMethodMap<TOAS, TPath>[TMethod] extends {
> = (OASMethodMap<TOAS, TPath>[TMethod] extends {
requestBody: { content: { 'application/json': { schema: JSONSchema } } };
}
? OASMethodMap<TOAS, TPath>[TMethod]['requestBody'] extends { required: true }
Expand All @@ -239,102 +239,103 @@ export type OASRequestParams<
OASMethodMap<TOAS, TPath>[TMethod]['requestBody']['content']['application/json']['schema']
>;
}
: {} & (OASMethodMap<TOAS, TPath>[TMethod] extends { parameters: { name: string; in: string }[] }
? OASParamMap<OASMethodMap<TOAS, TPath>[TMethod]['parameters']>
: {}) &
// If there is any parameters defined in path but not in the parameters array, we should add them to the params
(TPath extends `${string}{${string}}${string}`
? { params: Record<ExtractPathParamsWithBrackets<TPath>, string | number> }
: {}) &
(TPath extends `${string}:${string}${string}`
? { params: Record<ExtractPathParamsWithPattern<TPath>, string | number> }
: {}) &
// Respect security definitions in path object
(OASMethodMap<TOAS, TPath>[TMethod] extends {
security: { [key: string]: any }[];
: {}) &
(OASMethodMap<TOAS, TPath>[TMethod] extends { parameters: { name: string; in: string }[] }
? OASParamMap<OASMethodMap<TOAS, TPath>[TMethod]['parameters']>
: {}) &
// If there is any parameters defined in path but not in the parameters array, we should add them to the params
(TPath extends `${string}{${string}}${string}`
? { params: Record<ExtractPathParamsWithBrackets<TPath>, string | number> }
: {}) &
(TPath extends `${string}:${string}${string}`
? { params: Record<ExtractPathParamsWithPattern<TPath>, string | number> }
: {}) &
// Respect security definitions in path object
(OASMethodMap<TOAS, TPath>[TMethod] extends {
security: { [key: string]: any }[];
}
? TOAS extends {
components: {
securitySchemes: {
[TSecuritySchemeNameKey in SecuritySchemeName<
OASMethodMap<TOAS, TPath>[TMethod]
> extends string
? SecuritySchemeName<OASMethodMap<TOAS, TPath>[TMethod]>
: never]: infer TSecurityScheme;
};
};
}
? TOAS extends {
components: {
securitySchemes: {
[TSecuritySchemeNameKey in SecuritySchemeName<
OASMethodMap<TOAS, TPath>[TMethod]
> extends string
? SecuritySchemeName<OASMethodMap<TOAS, TPath>[TMethod]>
: never]: infer TSecurityScheme;
};
};
}
? OASSecurityParams<TSecurityScheme>
: {}
: {}) &
// Respect global security definitions
(TOAS extends { security: { [key: string]: any }[] }
? TOAS extends {
components: {
securitySchemes: {
[TSecuritySchemeNameKey in SecuritySchemeName<TOAS> extends string
? SecuritySchemeName<TOAS>
: never]: infer TSecurityScheme;
};
};
}
? OASSecurityParams<TSecurityScheme>
: {}
: {}) &
// Respect old swagger security definitions
(TOAS extends { security: { [key: string]: any }[] }
? TOAS extends {
securityDefinitions: {
[TSecuritySchemeNameKey in SecuritySchemeName<TOAS> extends string
? SecuritySchemeName<TOAS>
: never]: infer TSecurityScheme;
};
}
? OASSecurityParams<TSecurityScheme>
: {}
: {}) &
(OASMethodMap<TOAS, TPath>[TMethod] extends {
requestBody: { content: { 'multipart/form-data': { schema: JSONSchema } } };
? OASSecurityParams<TSecurityScheme>
: {}
: {}) &
// Respect global security definitions
(TOAS extends { security: { [key: string]: any }[] }
? TOAS extends {
components: {
securitySchemes: {
[TSecuritySchemeNameKey in SecuritySchemeName<TOAS> extends string
? SecuritySchemeName<TOAS>
: never]: infer TSecurityScheme;
};
};
}
? OASMethodMap<TOAS, TPath>[TMethod]['requestBody'] extends { required: true }
? {
formData: FromSchema<
OASMethodMap<
TOAS,
TPath
>[TMethod]['requestBody']['content']['multipart/form-data']['schema']
>;
}
: {
formData?: FromSchema<
OASMethodMap<
TOAS,
TPath
>[TMethod]['requestBody']['content']['multipart/form-data']['schema']
>;
}
: {}) &
(OASMethodMap<TOAS, TPath>[TMethod] extends {
requestBody: { content: { 'application/x-www-form-urlencoded': { schema: JSONSchema } } };
? OASSecurityParams<TSecurityScheme>
: {}
: {}) &
// Respect old swagger security definitions
(TOAS extends { security: { [key: string]: any }[] }
? TOAS extends {
securityDefinitions: {
[TSecuritySchemeNameKey in SecuritySchemeName<TOAS> extends string
? SecuritySchemeName<TOAS>
: never]: infer TSecurityScheme;
};
}
? OASMethodMap<TOAS, TPath>[TMethod]['requestBody'] extends { required: true }
? {
formUrlEncoded: FromSchema<
OASMethodMap<
TOAS,
TPath
>[TMethod]['requestBody']['content']['application/x-www-form-urlencoded']['schema']
>;
}
: {
formUrlEncoded?: FromSchema<
OASMethodMap<
TOAS,
TPath
>[TMethod]['requestBody']['content']['application/x-www-form-urlencoded']['schema']
>;
}
: {});
? OASSecurityParams<TSecurityScheme>
: {}
: {}) &
(OASMethodMap<TOAS, TPath>[TMethod] extends {
requestBody: { content: { 'multipart/form-data': { schema: JSONSchema } } };
}
? OASMethodMap<TOAS, TPath>[TMethod]['requestBody'] extends { required: true }
? {
formData: FromSchema<
OASMethodMap<
TOAS,
TPath
>[TMethod]['requestBody']['content']['multipart/form-data']['schema']
>;
}
: {
formData?: FromSchema<
OASMethodMap<
TOAS,
TPath
>[TMethod]['requestBody']['content']['multipart/form-data']['schema']
>;
}
: {}) &
(OASMethodMap<TOAS, TPath>[TMethod] extends {
requestBody: { content: { 'application/x-www-form-urlencoded': { schema: JSONSchema } } };
}
? OASMethodMap<TOAS, TPath>[TMethod]['requestBody'] extends { required: true }
? {
formUrlEncoded: FromSchema<
OASMethodMap<
TOAS,
TPath
>[TMethod]['requestBody']['content']['application/x-www-form-urlencoded']['schema']
>;
}
: {
formUrlEncoded?: FromSchema<
OASMethodMap<
TOAS,
TPath
>[TMethod]['requestBody']['content']['application/x-www-form-urlencoded']['schema']
>;
}
: {});

export type OASInput<
TOAS extends OpenAPIDocument,
Expand Down

0 comments on commit aaf3fd8

Please sign in to comment.