Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiment with Zod #47

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ root = true

[*]
charset = utf-8
end_of_line = crlf
end_of_line = lf
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was causing prettier to rewrite the file I was working on to replace the existing \n with \n\r. Apologies for the tooling churn here, feel free to tell me to revert! Otherwise, I can contribute a small whitespace PR that normalizes on crlf.

indent_size = 2
indent_style = space
insert_final_newline = true
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
"jsonwebtoken": "^9.0.1",
"playwright": "^1.36.0",
"yargs": "^17.7.2",
"zip-local": "^0.3.5"
"zip-local": "^0.3.5",
"zod": "^3.21.4"
}
}
17 changes: 12 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 43 additions & 43 deletions src/stores/edge/edge-input.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,47 @@
import { z } from "zod";
import { deployToEdgePublishApi } from "./edge-deploy.js";
import { getCorrectZip, getFullPath, getIsFileExists } from "../../utils.js";

const messageObtain = "To obtain one, follow https://github.com/avi12/web-ext-deploy/blob/main/EDGE_PUBLISH_API.md";

const EdgeOptionsPublishApiSchema = z.object({
clientId: z.string({
required_error: getErrorMessage(`No client ID is provided. ${messageObtain}`)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left these messages as is, but Zod provides the path to the issue as part of it's error structure, so you can consider just leaning on that.

}),
clientSecret: z.string({
required_error: getErrorMessage(`No client secret is provided. ${messageObtain}`)
}),
accessTokenUrl: z.string({
required_error: getErrorMessage(`No access token URL is provided. ${messageObtain}`)
}),
accessToken: z.string({
required_error: getErrorMessage(`No access token is provided. ${messageObtain}`)
}),
productId: z.string({
required_error: getErrorMessage(
"No product ID is provided, e.g. https://partner.microsoft.com/en-us/dashboard/microsoftedge/PRODUCT_ID"
)
}),
zip: z
.string({
required_error: getErrorMessage("No zip is provided")
})
.transform(getCorrectZip)
.superRefine((val, ctx) => {
if (!getIsFileExists(val)) {
ctx.addIssue({
message: getErrorMessage(`Zip doesn't exist: ${getFullPath(val)}`),
code: z.ZodIssueCode.custom
});
}
}),
devChangelog: z
.string()
.transform(cl => cl.replace(/\/\n/g, "\n"))
.optional(),
verbose: z.boolean().optional()
});

export class EdgeOptionsPublishApi {
/**
* The client ID.<br>
Expand Down Expand Up @@ -46,40 +87,7 @@ export class EdgeOptionsPublishApi {
verbose?: boolean;

constructor(options: EdgeOptionsPublishApi) {
if (!options.productId) {
throw new Error(
getErrorMessage(
"No product ID is provided, e.g. https://partner.microsoft.com/en-us/dashboard/microsoftedge/PRODUCT_ID"
)
);
}

const messageObtain = "To obtain one, follow https://github.com/avi12/web-ext-deploy/blob/main/EDGE_PUBLISH_API.md";

if (!options.clientId) {
throw new Error(getErrorMessage(`No client ID is provided. ${messageObtain}`));
}

if (!options.clientSecret) {
throw new Error(getErrorMessage(`No client secret is provided. ${messageObtain}`));
}

if (!options.accessTokenUrl) {
throw new Error(getErrorMessage(`No access token URL is provided. ${messageObtain}`));
}

if (!options.accessToken) {
throw new Error(getErrorMessage(`No access token is provided. ${messageObtain}`));
}

// Zip checking
if (!options.zip) {
throw new Error(getErrorMessage("No zip is provided"));
}

if (!getIsFileExists(options.zip)) {
throw new Error(getErrorMessage(`Zip doesn't exist: ${getFullPath(options.zip)}`));
}
Object.assign(this, EdgeOptionsPublishApiSchema.parse(options));
}
}

Expand All @@ -88,13 +96,5 @@ function getErrorMessage(message: string): string {
}

export async function prepareToDeployEdgePublishApi(options: EdgeOptionsPublishApi): Promise<boolean> {
options.zip = getCorrectZip(options.zip);

if (options.devChangelog) {
options.devChangelog = options.devChangelog.replace(/\/\n/g, "\n");
}

// Validate the options
new EdgeOptionsPublishApi(options);
return deployToEdgePublishApi(options);
return deployToEdgePublishApi(new EdgeOptionsPublishApi(options));
}