Skip to content

Commit

Permalink
feat(templates): add config handling in operations in the ts template
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandryackovlev committed Dec 26, 2019
1 parent cb42726 commit f1d7a46
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 45 deletions.
9 changes: 6 additions & 3 deletions src/templates/ts/index.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const ajv = new Ajv({ unknownFormats: ['int32', 'int64', 'binary']});
}
{{/if}}
{{#if (or query header params data)}}
export const {{name}}: (request: {{capitalize name}}Request, config?: Config) => Promise<{{capitalize name}}Response> = async ({
export const {{name}}: (request: {{capitalize name}}Request, config?: Partial<Config>) => Promise<{{capitalize name}}Response> = async ({
{{#if query}}
query = {},
{{/if}}
Expand All @@ -43,16 +43,19 @@ const ajv = new Ajv({ unknownFormats: ['int32', 'int64', 'binary']});
{{#if header}}
headers = {},
{{/if}}
} = {}, config = defaultConfig) => {
} = {}, currentConfig = defaultConfig) => {
{{else}}
export const {{name}}: (config?: Config) => Promise<{{capitalize name}}Response> = async (config = defaultConfig) => {
export const {{name}}: (config?: Partial<Config>) => Promise<{{capitalize name}}Response> = async (currentConfig = defaultConfig) => {
{{/if}}
const config = { ...defaultConfig, ...currentConfig };

{{#if (or query params)}}
let requestUrl = `${config.baseUrl}{{path}}`;
{{else}}
const requestUrl = `${config.baseUrl}{{path}}`;
{{/if}}


{{> queryString operation=this options=../options }}

{{> parameters operation=this options=../options }}
Expand Down
136 changes: 94 additions & 42 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,10 @@ export interface GetPetByIdRequest {
}
export const getPetById: (
request: GetPetByIdRequest,
config?: Config
) => Promise<GetPetByIdResponse> = async ({ params = {} } = {}, config = defaultConfig) => {
config?: Partial<Config>
) => Promise<GetPetByIdResponse> = async ({ params = {} } = {}, currentConfig = defaultConfig) => {
const config = { ...defaultConfig, ...currentConfig };
let requestUrl = \`\${config.baseUrl}/pet/{petId}\`;
const paramsSchema = {
Expand Down Expand Up @@ -550,11 +552,13 @@ export interface UpdatePetWithFormRequest {
}
export const updatePetWithForm: (
request: UpdatePetWithFormRequest,
config?: Config
config?: Partial<Config>
) => Promise<UpdatePetWithFormResponse> = async (
{ params = {}, data = {} } = {},
config = defaultConfig
currentConfig = defaultConfig
) => {
const config = { ...defaultConfig, ...currentConfig };
let requestUrl = \`\${config.baseUrl}/pet/{petId}\`;
const paramsSchema = {
Expand Down Expand Up @@ -654,11 +658,13 @@ export interface DeletePetRequest {
}
export const deletePet: (
request: DeletePetRequest,
config?: Config
config?: Partial<Config>
) => Promise<DeletePetResponse> = async (
{ params = {}, headers = {} } = {},
config = defaultConfig
currentConfig = defaultConfig
) => {
const config = { ...defaultConfig, ...currentConfig };
let requestUrl = \`\${config.baseUrl}/pet/{petId}\`;
const paramsSchema = {
Expand Down Expand Up @@ -756,11 +762,13 @@ export interface UploadFileRequest {
}
export const uploadFile: (
request: UploadFileRequest,
config?: Config
config?: Partial<Config>
) => Promise<UploadFileResponse> = async (
{ params = {}, data = {} } = {},
config = defaultConfig
currentConfig = defaultConfig
) => {
const config = { ...defaultConfig, ...currentConfig };
let requestUrl = \`\${config.baseUrl}/pet/{petId}/uploadImage\`;
const paramsSchema = {
Expand Down Expand Up @@ -883,8 +891,10 @@ export interface UpdatePetRequest {
}
export const updatePet: (
request: UpdatePetRequest,
config?: Config
) => Promise<UpdatePetResponse> = async ({ data = {} } = {}, config = defaultConfig) => {
config?: Partial<Config>
) => Promise<UpdatePetResponse> = async ({ data = {} } = {}, currentConfig = defaultConfig) => {
const config = { ...defaultConfig, ...currentConfig };
const requestUrl = \`\${config.baseUrl}/pet\`;
const requestHeaders = {
Expand Down Expand Up @@ -980,10 +990,12 @@ export const updatePet: (
export interface AddPetRequest {
data?: FormData | AddPetRequestBodyXml;
}
export const addPet: (request: AddPetRequest, config?: Config) => Promise<AddPetResponse> = async (
{ data = {} } = {},
config = defaultConfig
) => {
export const addPet: (
request: AddPetRequest,
config?: Partial<Config>
) => Promise<AddPetResponse> = async ({ data = {} } = {}, currentConfig = defaultConfig) => {
const config = { ...defaultConfig, ...currentConfig };
const requestUrl = \`\${config.baseUrl}/pet\`;
const requestHeaders = {
Expand Down Expand Up @@ -1081,8 +1093,13 @@ export interface FindPetsByStatusRequest {
}
export const findPetsByStatus: (
request: FindPetsByStatusRequest,
config?: Config
) => Promise<FindPetsByStatusResponse> = async ({ query = {} } = {}, config = defaultConfig) => {
config?: Partial<Config>
) => Promise<FindPetsByStatusResponse> = async (
{ query = {} } = {},
currentConfig = defaultConfig
) => {
const config = { ...defaultConfig, ...currentConfig };
let requestUrl = \`\${config.baseUrl}/pet/findByStatus\`;
const querySchema = {
Expand Down Expand Up @@ -1204,8 +1221,13 @@ export interface FindPetsByTagsRequest {
}
export const findPetsByTags: (
request: FindPetsByTagsRequest,
config?: Config
) => Promise<FindPetsByTagsResponse> = async ({ query = {} } = {}, config = defaultConfig) => {
config?: Partial<Config>
) => Promise<FindPetsByTagsResponse> = async (
{ query = {} } = {},
currentConfig = defaultConfig
) => {
const config = { ...defaultConfig, ...currentConfig };
let requestUrl = \`\${config.baseUrl}/pet/findByTags\`;
const querySchema = {
Expand Down Expand Up @@ -1317,9 +1339,11 @@ export const findPetsByTags: (
return response;
};
export const getInventory: (config?: Config) => Promise<GetInventoryResponse> = async (
config = defaultConfig
export const getInventory: (config?: Partial<Config>) => Promise<GetInventoryResponse> = async (
currentConfig = defaultConfig
) => {
const config = { ...defaultConfig, ...currentConfig };
const requestUrl = \`\${config.baseUrl}/store/inventory\`;
const requestHeaders = {
Expand Down Expand Up @@ -1380,8 +1404,13 @@ export interface GetOrderByIdRequest {
}
export const getOrderById: (
request: GetOrderByIdRequest,
config?: Config
) => Promise<GetOrderByIdResponse> = async ({ params = {} } = {}, config = defaultConfig) => {
config?: Partial<Config>
) => Promise<GetOrderByIdResponse> = async (
{ params = {} } = {},
currentConfig = defaultConfig
) => {
const config = { ...defaultConfig, ...currentConfig };
let requestUrl = \`\${config.baseUrl}/store/order/{orderId}\`;
const paramsSchema = {
Expand Down Expand Up @@ -1484,8 +1513,10 @@ export interface DeleteOrderRequest {
}
export const deleteOrder: (
request: DeleteOrderRequest,
config?: Config
) => Promise<DeleteOrderResponse> = async ({ params = {} } = {}, config = defaultConfig) => {
config?: Partial<Config>
) => Promise<DeleteOrderResponse> = async ({ params = {} } = {}, currentConfig = defaultConfig) => {
const config = { ...defaultConfig, ...currentConfig };
let requestUrl = \`\${config.baseUrl}/store/order/{orderId}\`;
const paramsSchema = {
Expand Down Expand Up @@ -1558,8 +1589,10 @@ export interface PlaceOrderRequest {
}
export const placeOrder: (
request: PlaceOrderRequest,
config?: Config
) => Promise<PlaceOrderResponse> = async ({ data = {} } = {}, config = defaultConfig) => {
config?: Partial<Config>
) => Promise<PlaceOrderResponse> = async ({ data = {} } = {}, currentConfig = defaultConfig) => {
const config = { ...defaultConfig, ...currentConfig };
const requestUrl = \`\${config.baseUrl}/store/order\`;
const requestHeaders = {
Expand Down Expand Up @@ -1670,8 +1703,13 @@ export interface GetUserByNameRequest {
}
export const getUserByName: (
request: GetUserByNameRequest,
config?: Config
) => Promise<GetUserByNameResponse> = async ({ params = {} } = {}, config = defaultConfig) => {
config?: Partial<Config>
) => Promise<GetUserByNameResponse> = async (
{ params = {} } = {},
currentConfig = defaultConfig
) => {
const config = { ...defaultConfig, ...currentConfig };
let requestUrl = \`\${config.baseUrl}/user/{username}\`;
const paramsSchema = {
Expand Down Expand Up @@ -1773,11 +1811,13 @@ export interface UpdateUserRequest {
}
export const updateUser: (
request: UpdateUserRequest,
config?: Config
config?: Partial<Config>
) => Promise<UpdateUserResponse> = async (
{ params = {}, data = {} } = {},
config = defaultConfig
currentConfig = defaultConfig
) => {
const config = { ...defaultConfig, ...currentConfig };
let requestUrl = \`\${config.baseUrl}/user/{username}\`;
const paramsSchema = {
Expand Down Expand Up @@ -1886,8 +1926,10 @@ export interface DeleteUserRequest {
}
export const deleteUser: (
request: DeleteUserRequest,
config?: Config
) => Promise<DeleteUserResponse> = async ({ params = {} } = {}, config = defaultConfig) => {
config?: Partial<Config>
) => Promise<DeleteUserResponse> = async ({ params = {} } = {}, currentConfig = defaultConfig) => {
const config = { ...defaultConfig, ...currentConfig };
let requestUrl = \`\${config.baseUrl}/user/{username}\`;
const paramsSchema = {
Expand Down Expand Up @@ -1960,8 +2002,10 @@ export interface LoginUserRequest {
}
export const loginUser: (
request: LoginUserRequest,
config?: Config
) => Promise<LoginUserResponse> = async ({ query = {} } = {}, config = defaultConfig) => {
config?: Partial<Config>
) => Promise<LoginUserResponse> = async ({ query = {} } = {}, currentConfig = defaultConfig) => {
const config = { ...defaultConfig, ...currentConfig };
let requestUrl = \`\${config.baseUrl}/user/login\`;
const querySchema = {
Expand Down Expand Up @@ -2038,9 +2082,11 @@ export const loginUser: (
return response;
};
export const logoutUser: (config?: Config) => Promise<LogoutUserResponse> = async (
config = defaultConfig
export const logoutUser: (config?: Partial<Config>) => Promise<LogoutUserResponse> = async (
currentConfig = defaultConfig
) => {
const config = { ...defaultConfig, ...currentConfig };
const requestUrl = \`\${config.baseUrl}/user/logout\`;
const requestHeaders = {
Expand Down Expand Up @@ -2083,8 +2129,10 @@ export interface CreateUserRequest {
}
export const createUser: (
request: CreateUserRequest,
config?: Config
) => Promise<CreateUserResponse> = async ({ data = {} } = {}, config = defaultConfig) => {
config?: Partial<Config>
) => Promise<CreateUserResponse> = async ({ data = {} } = {}, currentConfig = defaultConfig) => {
const config = { ...defaultConfig, ...currentConfig };
const requestUrl = \`\${config.baseUrl}/user\`;
const requestHeaders = {
Expand Down Expand Up @@ -2163,11 +2211,13 @@ export interface CreateUsersWithArrayInputRequest {
}
export const createUsersWithArrayInput: (
request: CreateUsersWithArrayInputRequest,
config?: Config
config?: Partial<Config>
) => Promise<CreateUsersWithArrayInputResponse> = async (
{ data = {} } = {},
config = defaultConfig
currentConfig = defaultConfig
) => {
const config = { ...defaultConfig, ...currentConfig };
const requestUrl = \`\${config.baseUrl}/user/createWithArray\`;
const requestHeaders = {
Expand Down Expand Up @@ -2249,11 +2299,13 @@ export interface CreateUsersWithListInputRequest {
}
export const createUsersWithListInput: (
request: CreateUsersWithListInputRequest,
config?: Config
config?: Partial<Config>
) => Promise<CreateUsersWithListInputResponse> = async (
{ data = {} } = {},
config = defaultConfig
currentConfig = defaultConfig
) => {
const config = { ...defaultConfig, ...currentConfig };
const requestUrl = \`\${config.baseUrl}/user/createWithList\`;
const requestHeaders = {
Expand Down

0 comments on commit f1d7a46

Please sign in to comment.