@prismicio/e2e-tests-utils 1.1.2 → 1.2.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.
- package/dist/clients/authenticationApi.cjs +1 -18
- package/dist/clients/authenticationApi.cjs.map +1 -1
- package/dist/clients/authenticationApi.d.ts +2 -3
- package/dist/clients/authenticationApi.js +2 -19
- package/dist/clients/authenticationApi.js.map +1 -1
- package/dist/clients/wroom.cjs +0 -26
- package/dist/clients/wroom.cjs.map +1 -1
- package/dist/clients/wroom.d.ts +2 -22
- package/dist/clients/wroom.js +0 -26
- package/dist/clients/wroom.js.map +1 -1
- package/dist/managers/repositories.cjs +17 -20
- package/dist/managers/repositories.cjs.map +1 -1
- package/dist/managers/repositories.d.ts +22 -12
- package/dist/managers/repositories.js +17 -20
- package/dist/managers/repositories.js.map +1 -1
- package/dist/managers/repository.cjs +32 -108
- package/dist/managers/repository.cjs.map +1 -1
- package/dist/managers/repository.d.ts +22 -50
- package/dist/managers/repository.js +32 -108
- package/dist/managers/repository.js.map +1 -1
- package/dist/types.d.ts +1 -18
- package/package.json +1 -3
- package/src/clients/authenticationApi.ts +3 -30
- package/src/clients/wroom.ts +2 -40
- package/src/managers/repositories.ts +37 -39
- package/src/managers/repository.ts +58 -148
- package/src/types.ts +1 -27
- package/dist/clients/manageV2.cjs +0 -145
- package/dist/clients/manageV2.cjs.map +0 -1
- package/dist/clients/manageV2.d.ts +0 -91
- package/dist/clients/manageV2.js +0 -145
- package/dist/clients/manageV2.js.map +0 -1
- package/src/clients/manageV2.ts +0 -178
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"repository.js","sources":["../../../src/managers/repository.ts"],"sourcesContent":["import { AxiosResponse } from \"axios\";\n\nimport {\n\tCustomType,\n\tSharedSlice,\n} from \"@prismicio/types-internal/lib/customtypes\";\n\nimport type { AuthenticationClient } from \"../clients/authenticationApi\";\nimport type { CoreClient } from \"../clients/coreApi\";\nimport { CustomTypesClient } from \"../clients/customTypesApi\";\nimport { ManageV2Client } from \"../clients/manageV2\";\nimport { WroomClient } from \"../clients/wroom\";\nimport { logHttpResponse, logger } from \"../utils/log\";\nimport { getRepositoryUrl } from \"../utils/urls\";\n\n/** Utility to manage test data for a Prismic repository */\nexport class RepositoryManager {\n\tconstructor(\n\t\treadonly name: string,\n\t\tprivate readonly coreApiClient: CoreClient,\n\t\tprivate readonly authApiClient: AuthenticationClient,\n\t\tprivate readonly wroomClient: WroomClient,\n\t\tprivate readonly customTypesApiClient: CustomTypesClient,\n\t\tprivate readonly manageV2Client: ManageV2Client,\n\t) {}\n\n\tasync configure(config: RepositoryConfig): Promise<void> {\n\t\tconst hasLangConfig =\n\t\t\tconfig.defaultLocale || config.locales || config.customLocale;\n\t\tif (hasLangConfig) {\n\t\t\tconst languages = await this.coreApiClient.getLanguages();\n\n\t\t\t// default locale must be set first\n\t\t\tif (config.defaultLocale) {\n\t\t\t\tconst master = languages.find(({ is_master }) => is_master === true);\n\t\t\t\tif (!master) {\n\t\t\t\t\tawait this.setDefaultLocale(config.defaultLocale);\n\t\t\t\t} else if (master.id !== config.defaultLocale) {\n\t\t\t\t\tlogger.warn(\n\t\t\t\t\t\t`Master locale is already set to '${master.id}', cannot update it to '${config.defaultLocale}'`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (config.locales) {\n\t\t\t\tconst localesToAdd = config.locales.filter(\n\t\t\t\t\t(id) => !languages.some((language) => language.id === id),\n\t\t\t\t);\n\t\t\t\tif (localesToAdd.length > 0) {\n\t\t\t\t\tawait this.createLocales(localesToAdd);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (config.customLocale) {\n\t\t\t\tif (!languages.find(({ id }) => id === config.customLocale?.lang.id)) {\n\t\t\t\t\tawait this.createCustomLocale(config.customLocale);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (config.slices) {\n\t\t\tawait this.createSlices(config.slices);\n\t\t}\n\n\t\tif (config.customTypes) {\n\t\t\tawait this.createCustomTypes(config.customTypes);\n\t\t}\n\n\t\tif (config.preview) {\n\t\t\tawait this.createPreview(config.preview);\n\t\t}\n\n\t\tif (config.rolePerLocal) {\n\t\t\tawait this.toggleRolePerLocal(true);\n\t\t}\n\t}\n\t/** @returns the repository base url, like https://my-repo.prismic.io */\n\tgetBaseURL(): string {\n\t\treturn getRepositoryUrl(this.wroomClient.getBaseURL(), this.name);\n\t}\n\n\t/**\n\t * Set additional standard locales for a repository in Wroom, does not fail if\n\t * the locales already exist.\n\t *\n\t * @param locales - An array of all locales to be added to the repository.\n\t *\n\t * @throws Error if setting the locales fails.\n\t */\n\tasync createLocales(locales: string[]): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t\"app/settings/multilanguages\",\n\t\t\t{ languages: locales },\n\t\t);\n\n\t\tconst { data, status } = response;\n\t\tconst success =\n\t\t\tstatus === 200 ||\n\t\t\t(status === 400 &&\n\t\t\t\t(data as string).includes(\"already existing languages\"));\n\t\tif (!success) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(`Could not add locales ${locales.join(\", \")}`);\n\t\t}\n\t\tprofiler.done({ message: `locales '${locales}' created` });\n\t}\n\n\t/**\n\t * Set additional custom locale for a repository in Wroom, does not fail if\n\t * the locales already exist.\n\t *\n\t * @param locale - The locale to be added to the repository.\n\t *\n\t * @throws Error if setting the locale fails.\n\t */\n\tasync createCustomLocale(locale: CustomLocale): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t\"app/settings/multilanguages/custom\",\n\t\t\tlocale,\n\t\t);\n\n\t\tconst { data, status } = response;\n\t\tconst success =\n\t\t\tstatus === 200 ||\n\t\t\t(status === 400 && JSON.stringify(data).includes(\"code is already used\"));\n\t\tif (!success) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(`Could not create custom locale ${locale}`);\n\t\t}\n\t\tprofiler.done({\n\t\t\tmessage: `custom locale ${JSON.stringify(locale)} created`,\n\t\t});\n\t}\n\n\tprivate failIfNot200(response: AxiosResponse, msg: string) {\n\t\tif (response.status !== 200) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(msg);\n\t\t}\n\t}\n\n\t/**\n\t * Delete a locale from a repository.\n\t *\n\t * @param locale - locale to remove from the repository configuration.\n\t *\n\t * @throws Error if deleting the locale fails.\n\t */\n\tasync deleteLocale(locale: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`app/settings/multilanguages/${locale}/delete`,\n\t\t);\n\t\tthis.failIfNot200(response, `Could not delete locale ${locale}`);\n\t\tprofiler.done({ message: `locale '${locale}' deleted` });\n\t}\n\n\t/**\n\t * Set the default (master) locale for a repository in Wroom, the locale needs\n\t * to exist for the repository.\n\t *\n\t * @param locale - The locale to be set as the default.\n\t *\n\t * @throws Error if setting the default locale fails.\n\t */\n\tasync setDefaultLocale(locale: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`/app/settings/multilanguages/${locale}/createMasterLang`,\n\t\t);\n\n\t\tconst { data, status } = response;\n\t\tconst success =\n\t\t\tstatus === 200 ||\n\t\t\t(status === 400 &&\n\t\t\t\t// returns 400 if master lang is already set\n\t\t\t\tJSON.stringify(data).includes(\"Unable to set as master language\"));\n\t\tif (!success) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(`Could not set default locale to ${locale}`);\n\t\t}\n\t\tprofiler.done({ message: `default locale set to '${locale}'` });\n\t}\n\n\t/**\n\t * Creates a release.\n\t *\n\t * @param label - The label for the release.\n\t *\n\t * @returns A Promise that resolves when the release is created.\n\t */\n\tasync createRelease(label: string): Promise<{ id: string }> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(this.name, \"app/releases\", {\n\t\t\tlabel,\n\t\t});\n\t\tthis.failIfNot200(response, `Could not create release ${label}`);\n\t\tprofiler.done({ message: `release '${label}' created` });\n\n\t\treturn response.data;\n\t}\n\n\t/**\n\t * Deletes a repository release.\n\t *\n\t * @param repository - The name of the repository.\n\t * @param id - The ID of the release to be deleted.\n\t */\n\tasync deleteRelease(id: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`app/releases/${id}/delete`,\n\t\t\t{},\n\t\t);\n\n\t\tthis.failIfNot200(response, `Could not delete release with id ${id}`);\n\t\tprofiler.done({ message: `release '${id}' deleted` });\n\t}\n\n\t/**\n\t * Creates a preview for a repository.\n\t *\n\t * @param data - The data for creating the preview.\n\t */\n\tasync createPreview(data: Preview): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`previews/new`,\n\t\t\tdata,\n\t\t);\n\n\t\tthis.failIfNot200(response, `Could not create preview ${data.name}`);\n\t\tprofiler.done({ message: `preview '${data.name}' created` });\n\n\t\treturn response.data;\n\t}\n\n\t/**\n\t * Deletes a preview from a repository.\n\t *\n\t * @param id - The ID of the preview to be deleted.\n\t */\n\tasync deletePreview(id: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`previews/delete/${id}`,\n\t\t\t{},\n\t\t);\n\n\t\tthis.failIfNot200(response, `Could not delete preview with id ${id}`);\n\t\tprofiler.done({ message: `preview '${id}' deleted` });\n\t}\n\n\t/**\n\t * Create Custom Types using the Custom types api.\n\t *\n\t * @param customTypes -\n\t */\n\tasync createCustomTypes(customTypes: CustomType[]): Promise<void> {\n\t\tawait this.customTypesApiClient.createCustomTypes(customTypes);\n\t}\n\n\t/**\n\t * Create slices using the Custom types api.\n\t *\n\t * @param slices -\n\t */\n\tasync createSlices(slices: SharedSlice[]): Promise<void> {\n\t\tawait this.customTypesApiClient.createSlices(slices);\n\t}\n\n\t/**\n\t * Create an access token to query private items of the Content api.\n\t *\n\t * @param appName - mandatory authorized application name\n\t */\n\tasync createContentAPIToken(appName: string): Promise<string> {\n\t\tconst profiler = logger.startTimer();\n\t\ttype AuthorizedAppResponse = {\n\t\t\tname: string;\n\t\t\twroom_auths: { token: string }[];\n\t\t};\n\t\tconst existingApps: AxiosResponse<AuthorizedAppResponse[]> =\n\t\t\tawait this.wroomClient.get(this.name, \"settings/security/contentapi\");\n\n\t\tthis.failIfNot200(\n\t\t\texistingApps,\n\t\t\t`Could not get status of existing applications to generate a permanent token`,\n\t\t);\n\t\tconst existingApp = existingApps.data.find(({ name, wroom_auths }) => {\n\t\t\treturn name === appName && wroom_auths.length > 0;\n\t\t});\n\t\tif (existingApp) {\n\t\t\treturn existingApp.wroom_auths[0].token;\n\t\t}\n\t\tconst response: AxiosResponse<AuthorizedAppResponse> =\n\t\t\tawait this.wroomClient.post(this.name, \"settings/security/oauthapp\", {\n\t\t\t\tapp_name: appName,\n\t\t\t});\n\n\t\tthis.failIfNot200(\n\t\t\tresponse,\n\t\t\t`Could not create permanent access token for app name ${appName}`,\n\t\t);\n\t\tprofiler.done({\n\t\t\tmessage: `permanent access token for app name '${appName}' created`,\n\t\t});\n\n\t\t// only return the generated token since there is no need to access more\n\t\treturn response.data.wroom_auths[0].token;\n\t}\n\n\t/** Create a permanent access token to authenticate to Prismic public apis. */\n\tasync createPermanentAccessToken(): Promise<string> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst token = await this.authApiClient.getMachine2MachineToken(this.name);\n\n\t\tprofiler.done({\n\t\t\tmessage: `machine2machine token created`,\n\t\t});\n\n\t\treturn token;\n\t}\n\n\t/**\n\t * Toggle the Role per local feature\n\t *\n\t * @param enabled - The feature new status\n\t */\n\tasync toggleRolePerLocal(enabled: boolean): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.manageV2Client.toggleRolePerLocal({\n\t\t\trepository: this.name,\n\t\t\tenabled: enabled,\n\t\t});\n\n\t\tthis.failIfNot200(\n\t\t\tresponse,\n\t\t\t`Could not ${enabled ? \"enable\" : \"disable\"} Role per local for ${\n\t\t\t\tthis.name\n\t\t\t}`,\n\t\t);\n\t\tprofiler.done({\n\t\t\tmessage: `Role per local ${enabled ? \"enabled\" : \"disabled\"}`,\n\t\t});\n\t}\n\n\t/**\n\t * Change the Repository Plan\n\t *\n\t * @param {string} newPlanId - The Id of the new Plan to apply\n\t */\n\tasync changePlan(newPlanId: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.manageV2Client.changePlan({\n\t\t\trepository: this.name,\n\t\t\tnewPlanId: newPlanId,\n\t\t\tbypassManualBilling: true, // For now the library does not support Stripe features\n\t\t});\n\n\t\tthis.failIfNot200(response, \"Could not change the Repository Plan\");\n\t\tprofiler.done({\n\t\t\tmessage: `Repository Plan changed to ${newPlanId}`,\n\t\t});\n\t}\n\n\t/**\n\t * Add a new user to the repository\n\t *\n\t * @param {string} email - The email of the user\n\t */\n\tasync addUser(email: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.manageV2Client.addUserToRepository({\n\t\t\trepository: this.name,\n\t\t\temail: email,\n\t\t});\n\n\t\tthis.failIfNot200(response, \"Could not add a new user to the repository\");\n\t\tprofiler.done({\n\t\t\tmessage: `User ${email} was added to the repository`,\n\t\t});\n\t}\n\n\t/**\n\t * Remove a user from the repository\n\t *\n\t * @param {string} email - The email of the user\n\t */\n\tasync removeUser(email: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.manageV2Client.removeUserFromRepository({\n\t\t\trepository: this.name,\n\t\t\temail: email,\n\t\t});\n\n\t\tthis.failIfNot200(\n\t\t\tresponse,\n\t\t\t`Could not remove the user ${email} from the repository`,\n\t\t);\n\t\tprofiler.done({\n\t\t\tmessage: `User ${email} was removed from the repository`,\n\t\t});\n\t}\n\n\t/**\n\t * Update the role of a user in a repository\n\t *\n\t * @param {string} email - The email of the user\n\t * @param {string | Record<string, string>} role - The new Role of the user,\n\t * either a string for basic workflow or an object such as `{ \"lang_id\":\n\t * \"Writer\" }`\n\t *\n\t * Example of roles are\n\t *\n\t * - Administrator\n\t * - Writer\n\t * - Contributor\n\t * - Manager (publisher)\n\t */\n\tasync updateUserRole(\n\t\temail: string,\n\t\trole: string | Record<string, string>,\n\t): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response =\n\t\t\ttypeof role === \"string\"\n\t\t\t\t? await this.wroomClient.updateRole(this.name, email, role)\n\t\t\t\t: await this.wroomClient.updateRolePerLocal(this.name, email, role);\n\n\t\tthis.failIfNot200(\n\t\t\tresponse,\n\t\t\t`Could not update the ${\n\t\t\t\ttypeof role === \"string\" ? \"role\" : \"rolePerLocal\"\n\t\t\t} of the user ${email}`,\n\t\t);\n\t\tprofiler.done({\n\t\t\tmessage: `Updated User's ${\n\t\t\t\ttypeof role === \"string\" ? \"role\" : \"rolePerLocal\"\n\t\t\t} for user ${email}`,\n\t\t});\n\t}\n}\n\nexport type RepositoryConfig = {\n\tcustomLocale?: CustomLocale;\n\tlocales?: string[];\n\tdefaultLocale?: string;\n\tslices?: SharedSlice[];\n\tcustomTypes?: CustomType[];\n\tpreview?: Preview;\n\trolePerLocal?: boolean;\n};\n\nexport type Preview = {\n\tname: string;\n\twebsiteURL: string;\n\tresolverPath: string;\n};\n\nexport type CustomLocale = {\n\tlang: {\n\t\tlabel: string;\n\t\tid: string;\n\t\tuseStandardAnalyzer?: boolean;\n\t};\n\tregion: {\n\t\tlabel: string;\n\t\tid: string;\n\t};\n};\n"],"names":[],"mappings":";;;;;;;;MAgBa,kBAAiB;AAAA,EAC7B,YACU,MACQ,eACA,eACA,aACA,sBACA,gBAA8B;AALtC;AACQ;AACA;AACA;AACA;AACA;AALR,SAAI,OAAJ;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AAA,MAC9D,YAAY,KAAK;AAAA,MACjB;AAAA,IAAA,CACA;AAEI,SAAA,aAAa,UAAU,4CAA4C;AACxE,aAAS,KAAK;AAAA,MACb,SAAS,QAAQ,KAAK;AAAA,IAAA,CACtB;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,WAAW,OAAa;AACvB,UAAA,WAAW,OAAO;AAExB,UAAM,WAAW,MAAM,KAAK,eAAe,yBAAyB;AAAA,MACnE,YAAY,KAAK;AAAA,MACjB;AAAA,IAAA,CACA;AAED,SAAK,aACJ,UACA,6BAA6B,KAAK,sBAAsB;AAEzD,aAAS,KAAK;AAAA,MACb,SAAS,QAAQ,KAAK;AAAA,IAAA,CACtB;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAiBA,MAAM,eACL,OACA,MAAqC;AAE/B,UAAA,WAAW,OAAO;AAElB,UAAA,WACL,OAAO,SAAS,WACb,MAAM,KAAK,YAAY,WAAW,KAAK,MAAM,OAAO,IAAI,IACxD,MAAM,KAAK,YAAY,mBAAmB,KAAK,MAAM,OAAO,IAAI;AAE/D,SAAA,aACJ,UACA,wBACC,OAAO,SAAS,WAAW,SAAS,cACrC,gBAAgB,KAAK,EAAE;AAExB,aAAS,KAAK;AAAA,MACb,SAAS,kBACR,OAAO,SAAS,WAAW,SAAS,cACrC,aAAa,KAAK;AAAA,IAAA,CAClB;AAAA,EACF;AACA;"}
|
|
1
|
+
{"version":3,"file":"repository.js","sources":["../../../src/managers/repository.ts"],"sourcesContent":["import { AxiosResponse } from \"axios\";\n\nimport {\n\tCustomType,\n\tSharedSlice,\n} from \"@prismicio/types-internal/lib/customtypes\";\n\nimport type { CoreClient } from \"../clients/coreApi\";\nimport { CustomTypesClient } from \"../clients/customTypesApi\";\nimport { WroomClient } from \"../clients/wroom\";\nimport { logHttpResponse, logger } from \"../utils/log\";\nimport { getRepositoryUrl } from \"../utils/urls\";\n\n/** Utility to manage test data for a Prismic repository */\nexport class RepositoryManager {\n\tconstructor(\n\t\treadonly name: string,\n\t\tprivate readonly coreApiClient: CoreClient,\n\t\tprivate readonly wroomClient: WroomClient,\n\t\tprivate readonly customTypesApiClient: CustomTypesClient,\n\t) {}\n\n\tasync configure(config: RepositoryConfig): Promise<void> {\n\t\tconst hasLangConfig =\n\t\t\tconfig.defaultLocale || config.locales || config.customLocale;\n\t\tif (hasLangConfig) {\n\t\t\tconst languages = await this.coreApiClient.getLanguages();\n\n\t\t\t// default locale must be set first\n\t\t\tif (config.defaultLocale) {\n\t\t\t\tconst master = languages.find(({ is_master }) => is_master === true);\n\t\t\t\tif (!master) {\n\t\t\t\t\tawait this.setDefaultLocale(config.defaultLocale);\n\t\t\t\t} else if (master.id !== config.defaultLocale) {\n\t\t\t\t\tlogger.warn(\n\t\t\t\t\t\t`Master locale is already set to '${master.id}', cannot update it to '${config.defaultLocale}'`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (config.locales) {\n\t\t\t\tconst localesToAdd = config.locales.filter(\n\t\t\t\t\t(id) => !languages.some((language) => language.id === id),\n\t\t\t\t);\n\t\t\t\tif (localesToAdd.length > 0) {\n\t\t\t\t\tawait this.createLocales(localesToAdd);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (config.customLocale) {\n\t\t\t\tif (!languages.find(({ id }) => id === config.customLocale?.lang.id)) {\n\t\t\t\t\tawait this.createCustomLocale(config.customLocale);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (config.slices) {\n\t\t\tawait this.createSlices(config.slices);\n\t\t}\n\n\t\tif (config.customTypes) {\n\t\t\tawait this.createCustomTypes(config.customTypes);\n\t\t}\n\n\t\tif (config.preview) {\n\t\t\tawait this.createPreview(config.preview);\n\t\t}\n\t}\n\t/** @returns the repository base url, like https://my-repo.prismic.io */\n\tgetBaseURL(): string {\n\t\treturn getRepositoryUrl(this.wroomClient.getBaseURL(), this.name);\n\t}\n\n\t/**\n\t * Set additional standard locales for a repository in Wroom, does not fail if\n\t * the locales already exist.\n\t *\n\t * @param locales - An array of all locales to be added to the repository.\n\t *\n\t * @throws Error if setting the locales fails.\n\t */\n\tasync createLocales(locales: string[]): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t\"app/settings/multilanguages\",\n\t\t\t{ languages: locales },\n\t\t);\n\n\t\tconst { data, status } = response;\n\t\tconst success =\n\t\t\tstatus === 200 ||\n\t\t\t(status === 400 &&\n\t\t\t\t(data as string).includes(\"already existing languages\"));\n\t\tif (!success) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(`Could not add locales ${locales.join(\", \")}`);\n\t\t}\n\t\tprofiler.done({ message: `locales '${locales}' created` });\n\t}\n\n\t/**\n\t * Set additional custom locale for a repository in Wroom, does not fail if\n\t * the locales already exist.\n\t *\n\t * @param locale - The locale to be added to the repository.\n\t *\n\t * @throws Error if setting the locale fails.\n\t */\n\tasync createCustomLocale(locale: CustomLocale): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t\"app/settings/multilanguages/custom\",\n\t\t\tlocale,\n\t\t);\n\n\t\tconst { data, status } = response;\n\t\tconst success =\n\t\t\tstatus === 200 ||\n\t\t\t(status === 400 && JSON.stringify(data).includes(\"code is already used\"));\n\t\tif (!success) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(`Could not create custom locale ${locale}`);\n\t\t}\n\t\tprofiler.done({\n\t\t\tmessage: `custom locale ${JSON.stringify(locale)} created`,\n\t\t});\n\t}\n\n\tprivate failIfNot200(response: AxiosResponse, msg: string) {\n\t\tif (response.status !== 200) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(msg);\n\t\t}\n\t}\n\n\t/**\n\t * Delete a locale from a repository.\n\t *\n\t * @param locale - locale to remove from the repository configuration.\n\t *\n\t * @throws Error if deleting the locale fails.\n\t */\n\tasync deleteLocale(locale: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`app/settings/multilanguages/${locale}/delete`,\n\t\t);\n\t\tthis.failIfNot200(response, `Could not delete locale ${locale}`);\n\t\tprofiler.done({ message: `locale '${locale}' deleted` });\n\t}\n\n\t/**\n\t * Set the default (master) locale for a repository in Wroom, the locale needs\n\t * to exist for the repository.\n\t *\n\t * @param locale - The locale to be set as the default.\n\t *\n\t * @throws Error if setting the default locale fails.\n\t */\n\tasync setDefaultLocale(locale: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`/app/settings/multilanguages/${locale}/createMasterLang`,\n\t\t);\n\n\t\tconst { data, status } = response;\n\t\tconst success =\n\t\t\tstatus === 200 ||\n\t\t\t(status === 400 &&\n\t\t\t\t// returns 400 if master lang is already set\n\t\t\t\tJSON.stringify(data).includes(\"Unable to set as master language\"));\n\t\tif (!success) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(`Could not set default locale to ${locale}`);\n\t\t}\n\t\tprofiler.done({ message: `default locale set to '${locale}'` });\n\t}\n\n\t/**\n\t * Creates a release.\n\t *\n\t * @param label - The label for the release.\n\t *\n\t * @returns A Promise that resolves when the release is created.\n\t */\n\tasync createRelease(label: string): Promise<{ id: string }> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(this.name, \"app/releases\", {\n\t\t\tlabel,\n\t\t});\n\t\tthis.failIfNot200(response, `Could not create release ${label}`);\n\t\tprofiler.done({ message: `release '${label}' created` });\n\n\t\treturn response.data;\n\t}\n\n\t/**\n\t * Deletes a repository release.\n\t *\n\t * @param repository - The name of the repository.\n\t * @param id - The ID of the release to be deleted.\n\t */\n\tasync deleteRelease(id: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`app/releases/${id}/delete`,\n\t\t\t{},\n\t\t);\n\n\t\tthis.failIfNot200(response, `Could not delete release with id ${id}`);\n\t\tprofiler.done({ message: `release '${id}' deleted` });\n\t}\n\n\t/**\n\t * Creates a preview for a repository.\n\t *\n\t * @param data - The data for creating the preview.\n\t */\n\tasync createPreview(data: Preview): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`previews/new`,\n\t\t\tdata,\n\t\t);\n\n\t\tthis.failIfNot200(response, `Could not create preview ${data.name}`);\n\t\tprofiler.done({ message: `preview '${data.name}' created` });\n\n\t\treturn response.data;\n\t}\n\n\t/**\n\t * Deletes a preview from a repository.\n\t *\n\t * @param id - The ID of the preview to be deleted.\n\t */\n\tasync deletePreview(id: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`previews/delete/${id}`,\n\t\t\t{},\n\t\t);\n\n\t\tthis.failIfNot200(response, `Could not delete preview with id ${id}`);\n\t\tprofiler.done({ message: `preview '${id}' deleted` });\n\t}\n\n\t/**\n\t * Create a webhook.\n\t *\n\t * @returns A Promise that resolves with the webhook id.\n\t */\n\tasync createWebhook(\n\t\tname: string,\n\t\turl: string,\n\t\tsecret: string,\n\t\ttriggers: {\n\t\t\tdocumentsPublished?: boolean;\n\t\t\tdocumentsUnpublished?: boolean;\n\t\t\treleasesCreated?: boolean;\n\t\t\treleasesUpdated?: boolean;\n\t\t\ttagsCreated?: boolean;\n\t\t\ttagsDeleted?: boolean;\n\t\t},\n\t\tactive: boolean,\n\t): Promise<string> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t\"app/settings/webhooks/create\",\n\t\t\t{\n\t\t\t\tname,\n\t\t\t\turl,\n\t\t\t\tsecret,\n\t\t\t\tactive: active ? \"on\" : \"off\",\n\t\t\t\t...triggers,\n\t\t\t},\n\t\t);\n\t\tthis.failIfNot200(response, `Could not create webhook ${name}`);\n\t\tprofiler.done({ message: `webhook '${name}' created` });\n\n\t\treturn response.data.created;\n\t}\n\n\t/**\n\t * Deletes a webhook.\n\t *\n\t * @param id - The webhook ID.\n\t */\n\tasync deleteWebhook(id: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`app/settings/webhooks/${id}/delete`,\n\t\t\t{},\n\t\t);\n\n\t\tthis.failIfNot200(response, `Could not delete webhook with id ${id}`);\n\t\tprofiler.done({ message: `webhook '${id}' deleted` });\n\t}\n\n\t/**\n\t * Create Custom Types using the Custom types api.\n\t *\n\t * @param customTypes -\n\t */\n\tasync createCustomTypes(customTypes: CustomType[]): Promise<void> {\n\t\tawait this.customTypesApiClient.createCustomTypes(customTypes);\n\t}\n\n\t/**\n\t * Create slices using the Custom types api.\n\t *\n\t * @param slices -\n\t */\n\tasync createSlices(slices: SharedSlice[]): Promise<void> {\n\t\tawait this.customTypesApiClient.createSlices(slices);\n\t}\n\n\t/**\n\t * Create a permanent access token along with an application name\n\t *\n\t * @param appName - mandatory authorized application name\n\t */\n\tasync createPermanentAccessToken(appName: string): Promise<string> {\n\t\tconst profiler = logger.startTimer();\n\t\ttype AuthorizedAppResponse = {\n\t\t\tname: string;\n\t\t\twroom_auths: { token: string }[];\n\t\t};\n\t\tconst existingApps: AxiosResponse<AuthorizedAppResponse[]> =\n\t\t\tawait this.wroomClient.get(this.name, \"settings/security/contentapi\");\n\n\t\tthis.failIfNot200(\n\t\t\texistingApps,\n\t\t\t`Could not get status of existing applications to generate a permanent token`,\n\t\t);\n\t\tconst existingApp = existingApps.data.find(({ name, wroom_auths }) => {\n\t\t\treturn name === appName && wroom_auths.length > 0;\n\t\t});\n\t\tif (existingApp) {\n\t\t\treturn existingApp.wroom_auths[0].token;\n\t\t}\n\t\tconst response: AxiosResponse<AuthorizedAppResponse> =\n\t\t\tawait this.wroomClient.post(this.name, \"settings/security/oauthapp\", {\n\t\t\t\tapp_name: appName,\n\t\t\t});\n\n\t\tthis.failIfNot200(\n\t\t\tresponse,\n\t\t\t`Could not create permanent access token for app name ${appName}`,\n\t\t);\n\t\tprofiler.done({\n\t\t\tmessage: `permanent access token for app name '${appName}' created`,\n\t\t});\n\n\t\t// only return the generated token since there is no need to access more\n\t\treturn response.data.wroom_auths[0].token;\n\t}\n}\n\nexport type RepositoryConfig = {\n\tcustomLocale?: CustomLocale;\n\tlocales?: string[];\n\tdefaultLocale?: string;\n\tslices?: SharedSlice[];\n\tcustomTypes?: CustomType[];\n\tpreview?: Preview;\n};\n\nexport type Preview = {\n\tname: string;\n\twebsiteURL: string;\n\tresolverPath: string;\n};\n\nexport type CustomLocale = {\n\tlang: {\n\t\tlabel: string;\n\t\tid: string;\n\t\tuseStandardAnalyzer?: boolean;\n\t};\n\tregion: {\n\t\tlabel: string;\n\t\tid: string;\n\t};\n};\n"],"names":[],"mappings":";;;;;;;;MAca,kBAAiB;AAAA,EAC7B,YACU,MACQ,eACA,aACA,sBAAuC;AAH/C;AACQ;AACA;AACA;AAHR,SAAI,OAAJ;AACQ,SAAa,gBAAb;AACA,SAAW,cAAX;AACA,SAAoB,uBAApB;AAAA,EACf;AAAA,EAEH,MAAM,UAAU,QAAwB;AACvC,UAAM,gBACL,OAAO,iBAAiB,OAAO,WAAW,OAAO;AAClD,QAAI,eAAe;AAClB,YAAM,YAAY,MAAM,KAAK,cAAc,aAAY;AAGvD,UAAI,OAAO,eAAe;AACnB,cAAA,SAAS,UAAU,KAAK,CAAC,EAAE,gBAAgB,cAAc,IAAI;AACnE,YAAI,CAAC,QAAQ;AACN,gBAAA,KAAK,iBAAiB,OAAO,aAAa;AAAA,QACtC,WAAA,OAAO,OAAO,OAAO,eAA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CAAC,EAAE,MAAM,kBAAiB;AAC7D,aAAA,SAAS,WAAW,YAAY,SAAS;AAAA,IAAA,CAChD;AACD,QAAI,aAAa;AACT,aAAA,YAAY,YAAY,CAAC,EAAE;AAAA,IAClC;AACD,UAAM,WACL,MAAM,KAAK,YAAY,KAAK,KAAK,MAAM,8BAA8B;AAAA,MACpE,UAAU;AAAA,IAAA,CACV;AAEF,SAAK,aACJ,UACA,wDAAwD,OAAO,EAAE;AAElE,aAAS,KAAK;AAAA,MACb,SAAS,wCAAwC,OAAO;AAAA,IAAA,CACxD;AAGD,WAAO,SAAS,KAAK,YAAY,CAAC,EAAE;AAAA,EACrC;AACA;"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,21 +1,4 @@
|
|
|
1
|
-
export type
|
|
2
|
-
/** Prismic base url */
|
|
3
|
-
baseURL: string;
|
|
4
|
-
/** Custom types api base url */
|
|
5
|
-
customTypesApi: string;
|
|
6
|
-
/** Auth service api base url */
|
|
7
|
-
authenticationApi: string;
|
|
8
|
-
};
|
|
9
|
-
export type AuthConfig = {
|
|
1
|
+
export type Credentials = {
|
|
10
2
|
email: string;
|
|
11
3
|
password: string;
|
|
12
4
|
};
|
|
13
|
-
export type ManageV2Config = {
|
|
14
|
-
secret: string;
|
|
15
|
-
audience: string;
|
|
16
|
-
};
|
|
17
|
-
export type SetupConfiguration = {
|
|
18
|
-
urlConfig: UrlConfig | string;
|
|
19
|
-
authConfig: AuthConfig;
|
|
20
|
-
manageV2Config?: ManageV2Config;
|
|
21
|
-
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prismicio/e2e-tests-utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "A collection of utilities for to manage Prismic test data",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -54,14 +54,12 @@
|
|
|
54
54
|
"dependencies": {
|
|
55
55
|
"@prismicio/types-internal": "^2.3.1",
|
|
56
56
|
"axios": "^1.6.2",
|
|
57
|
-
"jsonwebtoken": "^9.0.2",
|
|
58
57
|
"lodash.isequal": "^4.5.0",
|
|
59
58
|
"winston": "^3.11.0"
|
|
60
59
|
},
|
|
61
60
|
"devDependencies": {
|
|
62
61
|
"@size-limit/preset-small-lib": "^11.0.1",
|
|
63
62
|
"@trivago/prettier-plugin-sort-imports": "^4.2.0",
|
|
64
|
-
"@types/jsonwebtoken": "^9.0.6",
|
|
65
63
|
"@types/lodash.isequal": "^4.5.8",
|
|
66
64
|
"@types/node": "^20.9.2",
|
|
67
65
|
"@typescript-eslint/eslint-plugin": "^6.8.0",
|
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
import axios, { AxiosInstance } from "axios";
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { Credentials } from "../types";
|
|
4
4
|
|
|
5
5
|
import { logHttpResponse, logger } from "../utils/log";
|
|
6
6
|
|
|
7
7
|
export type AuthenticationClient = {
|
|
8
8
|
getToken: () => Promise<string>;
|
|
9
|
-
getMachine2MachineToken: (repository: string) => Promise<string>;
|
|
10
9
|
};
|
|
11
10
|
|
|
12
11
|
/** Client for interacting with the authentication service to manage tokens */
|
|
13
12
|
export const createAuthenticationApiClient = (
|
|
14
13
|
baseURL: string,
|
|
15
|
-
auth:
|
|
14
|
+
auth: Credentials,
|
|
16
15
|
): AuthenticationClient => {
|
|
17
16
|
let authToken: string;
|
|
18
17
|
|
|
@@ -40,7 +39,7 @@ export const createAuthenticationApiClient = (
|
|
|
40
39
|
}
|
|
41
40
|
|
|
42
41
|
/** Return an api user token. Creates one if needed. */
|
|
43
|
-
async function getToken()
|
|
42
|
+
async function getToken() {
|
|
44
43
|
if (!authToken) {
|
|
45
44
|
authToken = await login();
|
|
46
45
|
}
|
|
@@ -48,33 +47,7 @@ export const createAuthenticationApiClient = (
|
|
|
48
47
|
return authToken;
|
|
49
48
|
}
|
|
50
49
|
|
|
51
|
-
/** Return an api user token. Creates one if needed. */
|
|
52
|
-
async function getMachine2MachineToken(repository: string): Promise<string> {
|
|
53
|
-
if (!authToken) {
|
|
54
|
-
authToken = await login();
|
|
55
|
-
}
|
|
56
|
-
const result = await client.get<{ token: string; timestamp: number }>(
|
|
57
|
-
"machine2machine",
|
|
58
|
-
{
|
|
59
|
-
headers: {
|
|
60
|
-
Authorization: `Bearer ${authToken}`,
|
|
61
|
-
repository,
|
|
62
|
-
},
|
|
63
|
-
},
|
|
64
|
-
);
|
|
65
|
-
|
|
66
|
-
if (![200].includes(result.status) || !result.data.token) {
|
|
67
|
-
logHttpResponse(result);
|
|
68
|
-
throw new Error(
|
|
69
|
-
"Machine2Machine token generation failed, no token received.",
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return result.data.token;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
50
|
return {
|
|
77
51
|
getToken,
|
|
78
|
-
getMachine2MachineToken,
|
|
79
52
|
};
|
|
80
53
|
};
|
package/src/clients/wroom.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import axios, { AxiosInstance, AxiosResponse } from "axios";
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { Credentials } from "../types";
|
|
4
4
|
|
|
5
5
|
import { extractCookie } from "../utils/cookies";
|
|
6
6
|
import { logHttpResponse, logger } from "../utils/log";
|
|
@@ -20,7 +20,7 @@ export class WroomClient {
|
|
|
20
20
|
*/
|
|
21
21
|
constructor(
|
|
22
22
|
baseURL: string,
|
|
23
|
-
private readonly auth:
|
|
23
|
+
private readonly auth: Credentials,
|
|
24
24
|
) {
|
|
25
25
|
this.client = axios.create({
|
|
26
26
|
baseURL,
|
|
@@ -84,44 +84,6 @@ export class WroomClient {
|
|
|
84
84
|
return response;
|
|
85
85
|
}
|
|
86
86
|
|
|
87
|
-
/**
|
|
88
|
-
* Update the role of a user in the repository
|
|
89
|
-
*
|
|
90
|
-
* @param {string} repository - The repository name
|
|
91
|
-
* @param {string} email - The email of the user
|
|
92
|
-
* @param {string} role - The new role to be given
|
|
93
|
-
*
|
|
94
|
-
* @returns The Axios Reponse
|
|
95
|
-
*/
|
|
96
|
-
async updateRole(
|
|
97
|
-
repository: string,
|
|
98
|
-
email: string,
|
|
99
|
-
role: string,
|
|
100
|
-
): Promise<AxiosResponse> {
|
|
101
|
-
const path = `/app/settings/users/profiles?email=${email}&profile=${role}`;
|
|
102
|
-
|
|
103
|
-
return this.post(repository, path);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Update the role of a user in the repository
|
|
108
|
-
*
|
|
109
|
-
* @param {string} repository - The repository name
|
|
110
|
-
* @param {string} email - The email of the user
|
|
111
|
-
* @param {string} rolePerLocal - The role per local object
|
|
112
|
-
*
|
|
113
|
-
* @returns The Axios Reponse
|
|
114
|
-
*/
|
|
115
|
-
async updateRolePerLocal(
|
|
116
|
-
repository: string,
|
|
117
|
-
email: string,
|
|
118
|
-
rolePerLocal: Record<string, string>,
|
|
119
|
-
): Promise<AxiosResponse> {
|
|
120
|
-
const path = `/app/settings/users/rolesperlocale?email=${email}`;
|
|
121
|
-
|
|
122
|
-
return this.post(repository, path, rolePerLocal);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
87
|
/**
|
|
126
88
|
* Authenticate Wroom to call Wroom unofficial endpoints.
|
|
127
89
|
*
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Credentials } from "../types";
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
4
|
AuthenticationClient,
|
|
@@ -6,7 +6,6 @@ import {
|
|
|
6
6
|
} from "../clients/authenticationApi";
|
|
7
7
|
import { createCoreApiClient } from "../clients/coreApi";
|
|
8
8
|
import { createCustomTypesApiClient } from "../clients/customTypesApi";
|
|
9
|
-
import { ManageV2Client } from "../clients/manageV2";
|
|
10
9
|
import { WroomClient } from "../clients/wroom";
|
|
11
10
|
import { EnvVariableManager } from "../utils/envVariableManager";
|
|
12
11
|
import { logHttpResponse, logger } from "../utils/log";
|
|
@@ -19,29 +18,30 @@ import { RepositoryConfig, RepositoryManager } from "./repository";
|
|
|
19
18
|
* Factory method to create a RepositoriesManager to manage repositories test
|
|
20
19
|
* data.
|
|
21
20
|
*
|
|
22
|
-
* @param
|
|
23
|
-
* required to setup the RepositoriesManager.
|
|
21
|
+
* @param urlConfig - If provided as an object:
|
|
24
22
|
*
|
|
25
|
-
* - {@link
|
|
26
|
-
* - {@link
|
|
27
|
-
* - {@link
|
|
23
|
+
* - {@link UrlConfig.baseURL}: The Prismic base URL.
|
|
24
|
+
* - {@link UrlConfig.customTypesApi}: The base URL for the Custom Types API.
|
|
25
|
+
* - {@link UrlConfig.authenticationApi}: The base URL for the Authentication API.
|
|
26
|
+
*
|
|
27
|
+
* If provided as a string: It is treated as the base URL for Prismic. Other
|
|
28
|
+
* URLs (Custom Types API, Auth API) will be derived from it.
|
|
29
|
+
* @param auth - The authentication credentials
|
|
28
30
|
*
|
|
29
31
|
* @returns An instance of {@link RepositoriesManager} to manage test data.
|
|
30
32
|
*/
|
|
31
33
|
export const createRepositoriesManager = (
|
|
32
|
-
|
|
34
|
+
urlConfig: UrlConfig | string,
|
|
35
|
+
auth: Credentials,
|
|
33
36
|
): RepositoriesManager => {
|
|
34
|
-
return new RepositoriesManager(
|
|
37
|
+
return new RepositoriesManager(urlConfig, auth);
|
|
35
38
|
};
|
|
36
39
|
|
|
37
40
|
/** Utility object to manage Prismic test data */
|
|
38
41
|
export class RepositoriesManager {
|
|
39
|
-
private readonly
|
|
40
|
-
urlConfig: UrlConfig;
|
|
41
|
-
};
|
|
42
|
+
private readonly urls: UrlConfig;
|
|
42
43
|
private readonly wroomClient: WroomClient;
|
|
43
44
|
private readonly authClient: AuthenticationClient;
|
|
44
|
-
private readonly manageV2Client: ManageV2Client;
|
|
45
45
|
/**
|
|
46
46
|
* helper to keep track of repositories across independent test phases (setup,
|
|
47
47
|
* teardown)
|
|
@@ -51,31 +51,22 @@ export class RepositoriesManager {
|
|
|
51
51
|
);
|
|
52
52
|
|
|
53
53
|
/**
|
|
54
|
-
* @param
|
|
55
|
-
* required to setup the different clients.
|
|
54
|
+
* @param urlConfig - The base URL of the Wroom app.
|
|
56
55
|
* @param credentials - Authentication credentials (email and password)
|
|
57
56
|
*/
|
|
58
|
-
constructor(
|
|
59
|
-
|
|
60
|
-
|
|
57
|
+
constructor(
|
|
58
|
+
urlConfig: UrlConfig | string,
|
|
59
|
+
private readonly credentials: Credentials,
|
|
60
|
+
) {
|
|
61
|
+
if (typeof urlConfig === "string") {
|
|
61
62
|
// When a string is provided, treat it as the 'base' property
|
|
62
|
-
urlConfig
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
this.wroomClient = new WroomClient(
|
|
69
|
-
this.config.urlConfig.baseURL,
|
|
70
|
-
this.config.authConfig,
|
|
71
|
-
);
|
|
63
|
+
urlConfig = this.inferUrls(urlConfig);
|
|
64
|
+
}
|
|
65
|
+
this.urls = urlConfig;
|
|
66
|
+
this.wroomClient = new WroomClient(this.urls.baseURL, this.credentials);
|
|
72
67
|
this.authClient = createAuthenticationApiClient(
|
|
73
|
-
this.
|
|
74
|
-
this.
|
|
75
|
-
);
|
|
76
|
-
this.manageV2Client = new ManageV2Client(
|
|
77
|
-
this.config.urlConfig.baseURL,
|
|
78
|
-
this.config.manageV2Config,
|
|
68
|
+
this.urls.authenticationApi,
|
|
69
|
+
this.credentials,
|
|
79
70
|
);
|
|
80
71
|
}
|
|
81
72
|
|
|
@@ -158,7 +149,7 @@ export class RepositoriesManager {
|
|
|
158
149
|
const post = async () =>
|
|
159
150
|
this.wroomClient.post(repository, "app/settings/delete", {
|
|
160
151
|
confirm: repository,
|
|
161
|
-
password: this.
|
|
152
|
+
password: this.credentials.password,
|
|
162
153
|
});
|
|
163
154
|
const response = await post();
|
|
164
155
|
if (response.status !== 200) {
|
|
@@ -192,20 +183,27 @@ export class RepositoriesManager {
|
|
|
192
183
|
*/
|
|
193
184
|
getRepositoryManager(name: string): RepositoryManager {
|
|
194
185
|
const customTypeClient = createCustomTypesApiClient(
|
|
195
|
-
this.
|
|
186
|
+
this.urls.customTypesApi,
|
|
196
187
|
name,
|
|
197
188
|
this.authClient,
|
|
198
189
|
);
|
|
199
|
-
const coreApiUrl = getRepositoryUrl(this.
|
|
190
|
+
const coreApiUrl = getRepositoryUrl(this.urls.baseURL, name);
|
|
200
191
|
const coreApiClient = createCoreApiClient(coreApiUrl, this.authClient);
|
|
201
192
|
|
|
202
193
|
return new RepositoryManager(
|
|
203
194
|
name,
|
|
204
195
|
coreApiClient,
|
|
205
|
-
this.authClient,
|
|
206
196
|
this.wroomClient,
|
|
207
197
|
customTypeClient,
|
|
208
|
-
this.manageV2Client,
|
|
209
198
|
);
|
|
210
199
|
}
|
|
211
200
|
}
|
|
201
|
+
|
|
202
|
+
export type UrlConfig = {
|
|
203
|
+
/** Prismic base url */
|
|
204
|
+
baseURL: string;
|
|
205
|
+
/** Custom types api base url */
|
|
206
|
+
customTypesApi: string;
|
|
207
|
+
/** Auth service api base url */
|
|
208
|
+
authenticationApi: string;
|
|
209
|
+
};
|
|
@@ -5,10 +5,8 @@ import {
|
|
|
5
5
|
SharedSlice,
|
|
6
6
|
} from "@prismicio/types-internal/lib/customtypes";
|
|
7
7
|
|
|
8
|
-
import type { AuthenticationClient } from "../clients/authenticationApi";
|
|
9
8
|
import type { CoreClient } from "../clients/coreApi";
|
|
10
9
|
import { CustomTypesClient } from "../clients/customTypesApi";
|
|
11
|
-
import { ManageV2Client } from "../clients/manageV2";
|
|
12
10
|
import { WroomClient } from "../clients/wroom";
|
|
13
11
|
import { logHttpResponse, logger } from "../utils/log";
|
|
14
12
|
import { getRepositoryUrl } from "../utils/urls";
|
|
@@ -18,10 +16,8 @@ export class RepositoryManager {
|
|
|
18
16
|
constructor(
|
|
19
17
|
readonly name: string,
|
|
20
18
|
private readonly coreApiClient: CoreClient,
|
|
21
|
-
private readonly authApiClient: AuthenticationClient,
|
|
22
19
|
private readonly wroomClient: WroomClient,
|
|
23
20
|
private readonly customTypesApiClient: CustomTypesClient,
|
|
24
|
-
private readonly manageV2Client: ManageV2Client,
|
|
25
21
|
) {}
|
|
26
22
|
|
|
27
23
|
async configure(config: RepositoryConfig): Promise<void> {
|
|
@@ -69,10 +65,6 @@ export class RepositoryManager {
|
|
|
69
65
|
if (config.preview) {
|
|
70
66
|
await this.createPreview(config.preview);
|
|
71
67
|
}
|
|
72
|
-
|
|
73
|
-
if (config.rolePerLocal) {
|
|
74
|
-
await this.toggleRolePerLocal(true);
|
|
75
|
-
}
|
|
76
68
|
}
|
|
77
69
|
/** @returns the repository base url, like https://my-repo.prismic.io */
|
|
78
70
|
getBaseURL(): string {
|
|
@@ -268,6 +260,62 @@ export class RepositoryManager {
|
|
|
268
260
|
profiler.done({ message: `preview '${id}' deleted` });
|
|
269
261
|
}
|
|
270
262
|
|
|
263
|
+
/**
|
|
264
|
+
* Create a webhook.
|
|
265
|
+
*
|
|
266
|
+
* @returns A Promise that resolves with the webhook id.
|
|
267
|
+
*/
|
|
268
|
+
async createWebhook(
|
|
269
|
+
name: string,
|
|
270
|
+
url: string,
|
|
271
|
+
secret: string,
|
|
272
|
+
triggers: {
|
|
273
|
+
documentsPublished?: boolean;
|
|
274
|
+
documentsUnpublished?: boolean;
|
|
275
|
+
releasesCreated?: boolean;
|
|
276
|
+
releasesUpdated?: boolean;
|
|
277
|
+
tagsCreated?: boolean;
|
|
278
|
+
tagsDeleted?: boolean;
|
|
279
|
+
},
|
|
280
|
+
active: boolean,
|
|
281
|
+
): Promise<string> {
|
|
282
|
+
const profiler = logger.startTimer();
|
|
283
|
+
|
|
284
|
+
const response = await this.wroomClient.post(
|
|
285
|
+
this.name,
|
|
286
|
+
"app/settings/webhooks/create",
|
|
287
|
+
{
|
|
288
|
+
name,
|
|
289
|
+
url,
|
|
290
|
+
secret,
|
|
291
|
+
active: active ? "on" : "off",
|
|
292
|
+
...triggers,
|
|
293
|
+
},
|
|
294
|
+
);
|
|
295
|
+
this.failIfNot200(response, `Could not create webhook ${name}`);
|
|
296
|
+
profiler.done({ message: `webhook '${name}' created` });
|
|
297
|
+
|
|
298
|
+
return response.data.created;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Deletes a webhook.
|
|
303
|
+
*
|
|
304
|
+
* @param id - The webhook ID.
|
|
305
|
+
*/
|
|
306
|
+
async deleteWebhook(id: string): Promise<void> {
|
|
307
|
+
const profiler = logger.startTimer();
|
|
308
|
+
|
|
309
|
+
const response = await this.wroomClient.post(
|
|
310
|
+
this.name,
|
|
311
|
+
`app/settings/webhooks/${id}/delete`,
|
|
312
|
+
{},
|
|
313
|
+
);
|
|
314
|
+
|
|
315
|
+
this.failIfNot200(response, `Could not delete webhook with id ${id}`);
|
|
316
|
+
profiler.done({ message: `webhook '${id}' deleted` });
|
|
317
|
+
}
|
|
318
|
+
|
|
271
319
|
/**
|
|
272
320
|
* Create Custom Types using the Custom types api.
|
|
273
321
|
*
|
|
@@ -287,11 +335,11 @@ export class RepositoryManager {
|
|
|
287
335
|
}
|
|
288
336
|
|
|
289
337
|
/**
|
|
290
|
-
* Create
|
|
338
|
+
* Create a permanent access token along with an application name
|
|
291
339
|
*
|
|
292
340
|
* @param appName - mandatory authorized application name
|
|
293
341
|
*/
|
|
294
|
-
async
|
|
342
|
+
async createPermanentAccessToken(appName: string): Promise<string> {
|
|
295
343
|
const profiler = logger.startTimer();
|
|
296
344
|
type AuthorizedAppResponse = {
|
|
297
345
|
name: string;
|
|
@@ -326,143 +374,6 @@ export class RepositoryManager {
|
|
|
326
374
|
// only return the generated token since there is no need to access more
|
|
327
375
|
return response.data.wroom_auths[0].token;
|
|
328
376
|
}
|
|
329
|
-
|
|
330
|
-
/** Create a permanent access token to authenticate to Prismic public apis. */
|
|
331
|
-
async createPermanentAccessToken(): Promise<string> {
|
|
332
|
-
const profiler = logger.startTimer();
|
|
333
|
-
|
|
334
|
-
const token = await this.authApiClient.getMachine2MachineToken(this.name);
|
|
335
|
-
|
|
336
|
-
profiler.done({
|
|
337
|
-
message: `machine2machine token created`,
|
|
338
|
-
});
|
|
339
|
-
|
|
340
|
-
return token;
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
/**
|
|
344
|
-
* Toggle the Role per local feature
|
|
345
|
-
*
|
|
346
|
-
* @param enabled - The feature new status
|
|
347
|
-
*/
|
|
348
|
-
async toggleRolePerLocal(enabled: boolean): Promise<void> {
|
|
349
|
-
const profiler = logger.startTimer();
|
|
350
|
-
|
|
351
|
-
const response = await this.manageV2Client.toggleRolePerLocal({
|
|
352
|
-
repository: this.name,
|
|
353
|
-
enabled: enabled,
|
|
354
|
-
});
|
|
355
|
-
|
|
356
|
-
this.failIfNot200(
|
|
357
|
-
response,
|
|
358
|
-
`Could not ${enabled ? "enable" : "disable"} Role per local for ${
|
|
359
|
-
this.name
|
|
360
|
-
}`,
|
|
361
|
-
);
|
|
362
|
-
profiler.done({
|
|
363
|
-
message: `Role per local ${enabled ? "enabled" : "disabled"}`,
|
|
364
|
-
});
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
/**
|
|
368
|
-
* Change the Repository Plan
|
|
369
|
-
*
|
|
370
|
-
* @param {string} newPlanId - The Id of the new Plan to apply
|
|
371
|
-
*/
|
|
372
|
-
async changePlan(newPlanId: string): Promise<void> {
|
|
373
|
-
const profiler = logger.startTimer();
|
|
374
|
-
|
|
375
|
-
const response = await this.manageV2Client.changePlan({
|
|
376
|
-
repository: this.name,
|
|
377
|
-
newPlanId: newPlanId,
|
|
378
|
-
bypassManualBilling: true, // For now the library does not support Stripe features
|
|
379
|
-
});
|
|
380
|
-
|
|
381
|
-
this.failIfNot200(response, "Could not change the Repository Plan");
|
|
382
|
-
profiler.done({
|
|
383
|
-
message: `Repository Plan changed to ${newPlanId}`,
|
|
384
|
-
});
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
/**
|
|
388
|
-
* Add a new user to the repository
|
|
389
|
-
*
|
|
390
|
-
* @param {string} email - The email of the user
|
|
391
|
-
*/
|
|
392
|
-
async addUser(email: string): Promise<void> {
|
|
393
|
-
const profiler = logger.startTimer();
|
|
394
|
-
|
|
395
|
-
const response = await this.manageV2Client.addUserToRepository({
|
|
396
|
-
repository: this.name,
|
|
397
|
-
email: email,
|
|
398
|
-
});
|
|
399
|
-
|
|
400
|
-
this.failIfNot200(response, "Could not add a new user to the repository");
|
|
401
|
-
profiler.done({
|
|
402
|
-
message: `User ${email} was added to the repository`,
|
|
403
|
-
});
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
/**
|
|
407
|
-
* Remove a user from the repository
|
|
408
|
-
*
|
|
409
|
-
* @param {string} email - The email of the user
|
|
410
|
-
*/
|
|
411
|
-
async removeUser(email: string): Promise<void> {
|
|
412
|
-
const profiler = logger.startTimer();
|
|
413
|
-
|
|
414
|
-
const response = await this.manageV2Client.removeUserFromRepository({
|
|
415
|
-
repository: this.name,
|
|
416
|
-
email: email,
|
|
417
|
-
});
|
|
418
|
-
|
|
419
|
-
this.failIfNot200(
|
|
420
|
-
response,
|
|
421
|
-
`Could not remove the user ${email} from the repository`,
|
|
422
|
-
);
|
|
423
|
-
profiler.done({
|
|
424
|
-
message: `User ${email} was removed from the repository`,
|
|
425
|
-
});
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
/**
|
|
429
|
-
* Update the role of a user in a repository
|
|
430
|
-
*
|
|
431
|
-
* @param {string} email - The email of the user
|
|
432
|
-
* @param {string | Record<string, string>} role - The new Role of the user,
|
|
433
|
-
* either a string for basic workflow or an object such as `{ "lang_id":
|
|
434
|
-
* "Writer" }`
|
|
435
|
-
*
|
|
436
|
-
* Example of roles are
|
|
437
|
-
*
|
|
438
|
-
* - Administrator
|
|
439
|
-
* - Writer
|
|
440
|
-
* - Contributor
|
|
441
|
-
* - Manager (publisher)
|
|
442
|
-
*/
|
|
443
|
-
async updateUserRole(
|
|
444
|
-
email: string,
|
|
445
|
-
role: string | Record<string, string>,
|
|
446
|
-
): Promise<void> {
|
|
447
|
-
const profiler = logger.startTimer();
|
|
448
|
-
|
|
449
|
-
const response =
|
|
450
|
-
typeof role === "string"
|
|
451
|
-
? await this.wroomClient.updateRole(this.name, email, role)
|
|
452
|
-
: await this.wroomClient.updateRolePerLocal(this.name, email, role);
|
|
453
|
-
|
|
454
|
-
this.failIfNot200(
|
|
455
|
-
response,
|
|
456
|
-
`Could not update the ${
|
|
457
|
-
typeof role === "string" ? "role" : "rolePerLocal"
|
|
458
|
-
} of the user ${email}`,
|
|
459
|
-
);
|
|
460
|
-
profiler.done({
|
|
461
|
-
message: `Updated User's ${
|
|
462
|
-
typeof role === "string" ? "role" : "rolePerLocal"
|
|
463
|
-
} for user ${email}`,
|
|
464
|
-
});
|
|
465
|
-
}
|
|
466
377
|
}
|
|
467
378
|
|
|
468
379
|
export type RepositoryConfig = {
|
|
@@ -472,7 +383,6 @@ export type RepositoryConfig = {
|
|
|
472
383
|
slices?: SharedSlice[];
|
|
473
384
|
customTypes?: CustomType[];
|
|
474
385
|
preview?: Preview;
|
|
475
|
-
rolePerLocal?: boolean;
|
|
476
386
|
};
|
|
477
387
|
|
|
478
388
|
export type Preview = {
|