Skip to content

Commit

Permalink
chore: merge all status to one file
Browse files Browse the repository at this point in the history
  • Loading branch information
hosseinmd committed Mar 5, 2021
1 parent 98af46d commit 5825a3a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 51 deletions.
82 changes: 43 additions & 39 deletions src/mock/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,52 +21,56 @@ export const composeMockData = (
if (!res) {
return;
}
const val =
res["response"]?.["application/json"] ||
res["response"]?.["application/octet-stream"] ||
res["response"]?.["multipart/form-data"];

if (!val) {
return;
}
Object.entries(res["response"]).forEach(([status, content]) => {
const val =
content?.["application/json"] ||
content?.["application/octet-stream"] ||
content?.["multipart/form-data"];

if (val.example) {
response = val.example;
} else if (val.examples) {
const examplesKey = Object.keys(val.examples);
if (examplesKey.length <= 1) {
response = val.examples;
} else {
// for (const [key, example] of Object.entries<any>(val.examples)) {
// const extendedPathKey = pathKey + "_" + normalizeName(key);
// response = example["value"];
// }
if (!val) {
return;
}
} else if ("schema" in val) {
const { schema } = val;
const ref = schema[REF];
if (ref) {
const schemaName = getSchemaName(ref);
if (schemaName) {
response = schemas[schemaName];

if (val.example) {
response = val.example;
} else if (val.examples) {
const examplesKey = Object.keys(val.examples);
if (examplesKey.length <= 1) {
response = val.examples;
} else {
// for (const [key, example] of Object.entries<any>(val.examples)) {
// const extendedPathKey = pathKey + "_" + normalizeName(key);
// response = example["value"];
// }
}
} else {
if (isObject(schema)) {
response = parseObject(schema, schemas);
} else if (isArray(schema)) {
response = parseArray(schema, schemas);
} else if (schema.properties) {
response = schema.properties;
} else if (schema.type) {
response = DataType.defaultValue(schema);
} else if ("schema" in val) {
const { schema } = val;
const ref = schema[REF];
if (ref) {
const schemaName = getSchemaName(ref);
if (schemaName) {
response = schemas[schemaName];
}
} else {
if (isObject(schema)) {
response = parseObject(schema, schemas);
} else if (isArray(schema)) {
response = parseArray(schema, schemas);
} else if (schema.properties) {
response = schema.properties;
} else if (schema.type) {
response = DataType.defaultValue(schema);
}
}
}
}

ret[pathKey] = {
...res,
response,
};
ret[pathKey] = {
method: res.method,
path: res.path,
response: { [status]: response },
};
});
});
return ret;
};
25 changes: 13 additions & 12 deletions src/mock/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ export type ResponsesType = {
[path: string]: {
path: string;
method: Method;
response: SwaggerResponse["content"];
response: {
[status: string]: SwaggerResponse["content"];
};
};
};

Expand All @@ -26,19 +28,18 @@ export const extractResponses = (
Object.entries(value).forEach(
([method, options]: [string, SwaggerRequest]) => {
const { operationId, responses } = options;
const response: { [x: string]: any } = {};
Object.keys(responses).forEach((statusCode: string) => {
const response = responses[statusCode];
const { content } = response;
const key =
generateServiceName(path, method, operationId, config) +
`_${statusCode}`;

ret[key] = {
method: method as Method,
path,
response: content,
};
const { content } = responses[statusCode];
response[statusCode] = content;
});
const key = generateServiceName(path, method, operationId, config);

ret[key] = {
method: method as Method,
path,
response,
};
},
);
});
Expand Down

0 comments on commit 5825a3a

Please sign in to comment.