diff --git a/.editorconfig b/.editorconfig index 4a77cdf..95b32fe 100644 --- a/.editorconfig +++ b/.editorconfig @@ -2,7 +2,7 @@ root = true [*] charset = utf-8 -end_of_line = crlf +end_of_line = lf indent_size = 2 indent_style = space insert_final_newline = true diff --git a/package.json b/package.json index 390be98..eec5f12 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6c82f37..3023c4d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,9 +1,5 @@ lockfileVersion: '6.0' -settings: - autoInstallPeers: true - excludeLinksFromLockfile: false - dependencies: axios: specifier: ^1.4.0 @@ -47,6 +43,9 @@ dependencies: zip-local: specifier: ^0.3.5 version: 0.3.5 + zod: + specifier: ^3.21.4 + version: 3.21.4 devDependencies: '@types/jsonwebtoken': @@ -77,7 +76,7 @@ devDependencies: specifier: ^3.0.1 version: 3.0.1 prettier: - specifier: ^2.8.8 + specifier: 2.8.8 version: 2.8.8 rimraf: specifier: ^5.0.1 @@ -2467,3 +2466,11 @@ packages: jszip: 2.7.0 q: 1.5.1 dev: false + + /zod@3.21.4: + resolution: {integrity: sha512-m46AKbrzKVzOzs/DZgVnG5H55N1sv1M8qZU3A8RIKbs3mrACDNeIOeilDymVb2HdmP8uwshOCF4uJ8uM9rCqJw==} + dev: false + +settings: + autoInstallPeers: true + excludeLinksFromLockfile: false diff --git a/src/stores/edge/edge-input.ts b/src/stores/edge/edge-input.ts index 27cf215..cf9b08e 100644 --- a/src/stores/edge/edge-input.ts +++ b/src/stores/edge/edge-input.ts @@ -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}`) + }), + 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.
@@ -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)); } } @@ -88,13 +96,5 @@ function getErrorMessage(message: string): string { } export async function prepareToDeployEdgePublishApi(options: EdgeOptionsPublishApi): Promise { - 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)); }