counterfact 0.19.0 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,76 @@
1
+ # For most projects, this workflow file will not need changing; you simply need
2
+ # to commit it to your repository.
3
+ #
4
+ # You may wish to alter this file to override the set of languages analyzed,
5
+ # or to provide custom queries or build logic.
6
+ #
7
+ # ******** NOTE ********
8
+ # We have attempted to detect the languages in your repository. Please check
9
+ # the `language` matrix defined below to confirm you have the correct set of
10
+ # supported CodeQL languages.
11
+ #
12
+ name: "CodeQL"
13
+
14
+ on:
15
+ push:
16
+ branches: [ "main" ]
17
+ pull_request:
18
+ # The branches below must be a subset of the branches above
19
+ branches: [ "main" ]
20
+ schedule:
21
+ - cron: '43 18 * * 1'
22
+
23
+ jobs:
24
+ analyze:
25
+ name: Analyze
26
+ runs-on: ubuntu-latest
27
+ permissions:
28
+ actions: read
29
+ contents: read
30
+ security-events: write
31
+
32
+ strategy:
33
+ fail-fast: false
34
+ matrix:
35
+ language: [ 'javascript' ]
36
+ # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
37
+ # Use only 'java' to analyze code written in Java, Kotlin or both
38
+ # Use only 'javascript' to analyze code written in JavaScript, TypeScript or both
39
+ # Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support
40
+
41
+ steps:
42
+ - name: Checkout repository
43
+ uses: actions/checkout@v3
44
+
45
+ # Initializes the CodeQL tools for scanning.
46
+ - name: Initialize CodeQL
47
+ uses: github/codeql-action/init@v2
48
+ with:
49
+ languages: ${{ matrix.language }}
50
+ # If you wish to specify custom queries, you can do so here or in a config file.
51
+ # By default, queries listed here will override any specified in a config file.
52
+ # Prefix the list here with "+" to use these queries and those in the config file.
53
+
54
+ # Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
55
+ # queries: security-extended,security-and-quality
56
+
57
+
58
+ # Autobuild attempts to build any compiled languages (C/C++, C#, Go, or Java).
59
+ # If this step fails, then you should remove it and run the build manually (see below)
60
+ - name: Autobuild
61
+ uses: github/codeql-action/autobuild@v2
62
+
63
+ # ℹ️ Command-line programs to run using the OS shell.
64
+ # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
65
+
66
+ # If the Autobuild fails above, remove it and uncomment the following three lines.
67
+ # modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.
68
+
69
+ # - run: |
70
+ # echo "Run, Build Application using script"
71
+ # ./location_of_script_within_repo/buildscript.sh
72
+
73
+ - name: Perform CodeQL Analysis
74
+ uses: github/codeql-action/analyze@v2
75
+ with:
76
+ category: "/language:${{matrix.language}}"
package/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # counterfact
2
2
 
3
+ ## 0.20.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 3c78a07: support for enum values in schemas
8
+
9
+ ### Patch Changes
10
+
11
+ - b2bf1db: fix duplicate imports of Context object
12
+ - 0f4ead6: don't import HTTPStatusCode if not needed
13
+ - 8e4410c: redirect /counterfact to counterfact/
14
+
15
+ ## 0.19.1
16
+
17
+ ### Patch Changes
18
+
19
+ - ab730a8: fix an issue where top-level produces in OpenAPI 2 was not working
20
+
3
21
  ## 0.19.0
4
22
 
5
23
  ### Minor Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "counterfact",
3
- "version": "0.19.0",
3
+ "version": "0.20.0",
4
4
  "description": "a library for building a fake REST API for testing",
5
5
  "type": "module",
6
6
  "main": "./src/server/counterfact.js",
@@ -1,3 +1,4 @@
1
+ /* eslint-disable max-statements */
1
2
  import nodePath from "node:path";
2
3
  import { pathToFileURL } from "node:url";
3
4
 
@@ -105,6 +106,16 @@ export async function start({
105
106
  })
106
107
  );
107
108
 
109
+ app.use((ctx, next) => {
110
+ if (ctx.URL.pathname === "/counterfact") {
111
+ ctx.redirect("/counterfact/");
112
+
113
+ return;
114
+ }
115
+
116
+ next();
117
+ });
118
+
108
119
  app.use(
109
120
  page("/counterfact/rapidoc", "rapi-doc", {
110
121
  basePath,
@@ -11,6 +11,10 @@ export class ContextCoder extends Coder {
11
11
  .replaceAll("~0", "~");
12
12
  }
13
13
 
14
+ get id() {
15
+ return "ContextCoder";
16
+ }
17
+
14
18
  names() {
15
19
  return super.names("Context");
16
20
  }
@@ -35,7 +35,7 @@ export class OperationTypeCoder extends Coder {
35
35
  if (response.has("schema")) {
36
36
  const produces =
37
37
  this.requirement?.get("produces")?.data ??
38
- this.requirement.specification.requirementAt("#/produces").data;
38
+ this.requirement.rootRequirement.get("produces").data;
39
39
 
40
40
  return produces
41
41
  .map(
@@ -12,6 +12,8 @@ export class ResponseTypeCoder extends Coder {
12
12
  }
13
13
 
14
14
  typeForDefaultStatusCode(listedStatusCodes) {
15
+ this.needsHttpStatusCodeImport = true;
16
+
15
17
  const definedStatusCodes = listedStatusCodes.filter(
16
18
  (key) => key !== "default"
17
19
  );
@@ -101,10 +103,13 @@ export class ResponseTypeCoder extends Coder {
101
103
  "ResponseBuilderFactory",
102
104
  nodePath.join(basePath, "response-builder-factory.js")
103
105
  );
104
- script.importExternalType(
105
- "HttpStatusCode",
106
- nodePath.join(basePath, "response-builder-factory.js")
107
- );
106
+
107
+ if (this.needsHttpStatusCodeImport) {
108
+ script.importExternalType(
109
+ "HttpStatusCode",
110
+ nodePath.join(basePath, "response-builder-factory.js")
111
+ );
112
+ }
108
113
 
109
114
  return `ResponseBuilderFactory<${this.buildResponseObjectType(script)}>`;
110
115
  }
@@ -93,6 +93,12 @@ export class SchemaTypeCoder extends Coder {
93
93
  return types.join(allOf ? " & " : " | ");
94
94
  }
95
95
 
96
+ writeEnum(script, requirement) {
97
+ return requirement.data
98
+ .map((item) => (typeof item === "string" ? `"${item}"` : item))
99
+ .join(" | ");
100
+ }
101
+
96
102
  modulePath() {
97
103
  return `components/${this.requirement.data.$ref.split("/").at(-1)}.ts`;
98
104
  }
@@ -108,6 +114,10 @@ export class SchemaTypeCoder extends Coder {
108
114
  return this.writeGroup(script, { allOf, anyOf, oneOf });
109
115
  }
110
116
 
117
+ if (this.requirement.has("enum")) {
118
+ return this.writeEnum(script, this.requirement.get("enum"));
119
+ }
120
+
111
121
  return this.writeType(script, type);
112
122
  }
113
123
  }
@@ -36,6 +36,8 @@ export class Specification {
36
36
 
37
37
  this.cache.set(urlOrPath, data);
38
38
 
39
+ this.rootRequirement = new Requirement(data, `${urlOrPath}#`, this);
40
+
39
41
  return data;
40
42
  }
41
43
  }
@@ -0,0 +1,6 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`a ContextCoder finds the request method in the URL 1`] = `
4
+ "export { default } from \\"../$.context.js\\";
5
+ "
6
+ `;