Skip to content

Commit

Permalink
Added 'raw' option
Browse files Browse the repository at this point in the history
  • Loading branch information
Netfloex committed Jan 10, 2022
1 parent d898969 commit 4d65900
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/lib/createConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ const createLocation = async (location: Server): Promise<NginxLocation> => {
block.auth_basic_user_file = filename;
}

if (location.raw) {
Object.entries(location.raw).forEach(([key, value]) => {
block[key] = value;
});
}

return block;
};

Expand Down
17 changes: 13 additions & 4 deletions src/lib/validateConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ const returnKeys = [
"redirect",
"rewrite",
"html",
"static"
"static",
"raw"
];

const optionalReturnKeys = ["raw"];

export const jsUnion = z.union([z.string(), z.number(), z.boolean()]);

export const returnKeysFromOption = (
options: Record<string, unknown>
): string[] => Object.keys(options).filter((key) => returnKeys.includes(key));
Expand Down Expand Up @@ -81,7 +86,10 @@ const oneReturnRefinement =
});
}
}
if (keys.length > 1) {

if (
keys.filter((key) => !optionalReturnKeys.includes(key)).length > 1
) {
addIssue({
message: chalk`Too many "return" types, found: {yellow ${keys.join(
", "
Expand All @@ -98,7 +106,7 @@ export const locationSchema = z
custom_css: urlsOrUrlSchema,
custom_js: urlsOrUrlSchema,
return: z.string().or(z.number()),
headers: z.record(z.union([z.string(), z.number(), z.boolean()])),
headers: z.record(jsUnion),
cors: z
.boolean()
.transform((bool) => (bool ? "*" : false))
Expand All @@ -125,7 +133,8 @@ export const locationSchema = z
message: chalk`Static path does not exist: {dim ${path}}`
});
}
})
}),
raw: z.record(jsUnion)
})
.partial()
.strict();
Expand Down
1 change: 1 addition & 0 deletions src/models/nginx-config-parser.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ declare module "@webantic/nginx-config-parser" {

include?: string[];
alias?: string;
[T: string]: string[] | string | number | boolean;
}
class Parser {
public toJSON: <Config>(conf: string) => Config;
Expand Down
1 change: 1 addition & 0 deletions src/tests/__snapshots__/create.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ exports[`Create config Full Server 1`] = `
add_header Second Value;
auth_basic 2d77215cd91113df4d3442ee904fd740;
auth_basic_user_file /current/data/auth/2d77215cd91113df4d3442ee904fd740;
custom option;
}
location /custom_assets {
Expand Down
7 changes: 6 additions & 1 deletion src/tests/__snapshots__/validate.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ exports[`Validate Config It accepts a valid config 1`] = `
"proxy_pass": "http://example.com",
},
],
"raw": {
"custom": "option",
},
"subdomains": {
"no_return_allowed": {
"locations": [
Expand Down Expand Up @@ -108,6 +111,8 @@ exports[`Validate Config It checks for an invalid config 1`] = `
"[ERROR] Error: Username should not be empty",
"[ERROR] Path: servers.example.com.locations.extra.auth.password",
"[ERROR] Error: Required",
"[ERROR] Path: servers.example.com.subdomains.too_many",
"[ERROR] Error: Too many \\"return\\" types, found: redirect, html, raw, please use only one of them.",
"[ERROR] Path: servers.dns_error",
"[ERROR] Error: DNS lookup failed for: not-existing",
]
Expand All @@ -117,7 +122,7 @@ exports[`Validate Config It tests for invalid number of return types 1`] = `
[
"[ERROR] There was an issue with your config, the errors are listed below.",
"[ERROR] Path: servers.no returns",
"[ERROR] Error: Please specify at least one \\"return\\" type: proxy_pass, return, redirect, rewrite, html, static",
"[ERROR] Error: Please specify at least one \\"return\\" type: proxy_pass, return, redirect, rewrite, html, static, raw",
"[ERROR] Path: servers.example.com",
"[ERROR] Error: Too many \\"return\\" types, found: proxy_pass, redirect, rewrite, html, return, please use only one of them.",
]
Expand Down
10 changes: 9 additions & 1 deletion src/tests/configs/full-error-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@
"redirect": [],
"return": [],
"rewrite": [],
"subdomains": {}
"subdomains": {
"too_many": {
"raw": {
"three": "return keys"
},
"redirect": "/",
"html": "The raw option is an optional return key."
}
}
}
},
"unknown": ""
Expand Down
3 changes: 3 additions & 0 deletions src/tests/configs/full-valid-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
"proxy_pass": "http://example.com"
}
},
"raw": {
"custom": "option"
},
"subdomains": {
"www": "http://example.com",

Expand Down
5 changes: 4 additions & 1 deletion src/tests/configs/full-valid-server.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@
"redirect": "/redirect",
"return": "200 return directive",
"rewrite": "^ https://rewrite",
"websocket": true
"websocket": true,
"raw": {
"custom": "option"
}
}

0 comments on commit 4d65900

Please sign in to comment.