From 8d259bf505ac3b17306b33854cf978a8e5647411 Mon Sep 17 00:00:00 2001 From: zoubingwu Date: Fri, 5 Jul 2024 07:22:57 +0800 Subject: [PATCH] fix: only include ai code when enabled (#56) --- .changeset/six-elephants-poke.md | 5 +++++ src/template.ts | 10 +++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 .changeset/six-elephants-poke.md diff --git a/.changeset/six-elephants-poke.md b/.changeset/six-elephants-poke.md new file mode 100644 index 0000000..041300f --- /dev/null +++ b/.changeset/six-elephants-poke.md @@ -0,0 +1,5 @@ +--- +'msw-auto-mock': minor +--- + +Only output ai code when enabled diff --git a/src/template.ts b/src/template.ts index 918e687..3eaeae7 100644 --- a/src/template.ts +++ b/src/template.ts @@ -17,7 +17,7 @@ export const mockTemplate = (operationCollection: OperationCollection, baseURL: ${getImportsCode()} ${createAiGenerateText(options)} -${withCreatePrompt} +${withCreatePrompt(options)} faker.seed(1); @@ -105,13 +105,17 @@ async function ask(operation) { } `; -const withCreatePrompt = ` +const withCreatePrompt = (options: ConfigOptions) => + options.ai?.enable + ? ` function createPrompt(operation) { return "Given the following Swagger (OpenAPI) definition, generate a mock data object that conforms to the specified response structure, ensuring each property adheres to its defined constraints, especially type, format, example, description, enum values, ignore the xml field. The generated JSON string should include all the properties defined in the Swagger schema as much as possible and the values should be valid based on the property definitions (e.g., integer, string, length etc.) and rules (e.g, int64 should be string in json etc.). Please only return the JSON string as the output, don't wrap it with markdown. The definition is like below: \\n" + "\`\`\`json" + JSON.stringify(operation, null, 4) + "\\n\`\`\`"; } -`; +` + : ''; export function createAiGenerateText(options: ConfigOptions): string { + if (!options.ai?.enable) return ''; return match(options.ai?.provider) .with('openai', () => askOpenai(options)) .with('azure', () => askAzure(options))