@shopware-ag/app-server-sdk 1.1.21 → 1.1.23

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.
@@ -3,5 +3,5 @@ export { InMemoryShopRepository, SimpleShop } from "./repository.js";
3
3
  export type { ShopInterface, ShopRepositoryInterface } from "./repository.js";
4
4
  export { HttpClient, HttpClientResponse, ApiClientAuthenticationFailed, ApiClientRequestFailed, } from "./http-client.js";
5
5
  export { Context } from "./context-resolver.js";
6
- export { ShopAuthorizeEvent, AppInstallEvent, AppActivateEvent, AppDeactivateEvent, AppUpdateEvent, AppUninstallEvent, } from "./registration.js";
6
+ export { ShopAuthorizeEvent, AppInstallEvent, AppActivateEvent, AppDeactivateEvent, AppUpdateEvent, AppUninstallEvent, BeforeRegistrationEvent, } from "./registration.js";
7
7
  //# sourceMappingURL=mod.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AACrC,OAAO,EAAE,sBAAsB,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AACrE,YAAY,EAAE,aAAa,EAAE,uBAAuB,EAAE,MAAM,iBAAiB,CAAC;AAC9E,OAAO,EACN,UAAU,EACV,kBAAkB,EAClB,6BAA6B,EAC7B,sBAAsB,GACtB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAChD,OAAO,EACN,kBAAkB,EAClB,eAAe,EACf,gBAAgB,EAChB,kBAAkB,EAClB,cAAc,EACd,iBAAiB,GACjB,MAAM,mBAAmB,CAAC"}
1
+ {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AACrC,OAAO,EAAE,sBAAsB,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AACrE,YAAY,EAAE,aAAa,EAAE,uBAAuB,EAAE,MAAM,iBAAiB,CAAC;AAC9E,OAAO,EACN,UAAU,EACV,kBAAkB,EAClB,6BAA6B,EAC7B,sBAAsB,GACtB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAChD,OAAO,EACN,kBAAkB,EAClB,eAAe,EACf,gBAAgB,EAChB,kBAAkB,EAClB,cAAc,EACd,iBAAiB,EACjB,uBAAuB,GACvB,MAAM,mBAAmB,CAAC"}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AppUninstallEvent = exports.AppUpdateEvent = exports.AppDeactivateEvent = exports.AppActivateEvent = exports.AppInstallEvent = exports.ShopAuthorizeEvent = exports.Context = exports.ApiClientRequestFailed = exports.ApiClientAuthenticationFailed = exports.HttpClientResponse = exports.HttpClient = exports.SimpleShop = exports.InMemoryShopRepository = exports.AppServer = void 0;
3
+ exports.BeforeRegistrationEvent = exports.AppUninstallEvent = exports.AppUpdateEvent = exports.AppDeactivateEvent = exports.AppActivateEvent = exports.AppInstallEvent = exports.ShopAuthorizeEvent = exports.Context = exports.ApiClientRequestFailed = exports.ApiClientAuthenticationFailed = exports.HttpClientResponse = exports.HttpClient = exports.SimpleShop = exports.InMemoryShopRepository = exports.AppServer = void 0;
4
4
  var app_js_1 = require("./app.js");
5
5
  Object.defineProperty(exports, "AppServer", { enumerable: true, get: function () { return app_js_1.AppServer; } });
6
6
  var repository_js_1 = require("./repository.js");
@@ -20,4 +20,5 @@ Object.defineProperty(exports, "AppActivateEvent", { enumerable: true, get: func
20
20
  Object.defineProperty(exports, "AppDeactivateEvent", { enumerable: true, get: function () { return registration_js_1.AppDeactivateEvent; } });
21
21
  Object.defineProperty(exports, "AppUpdateEvent", { enumerable: true, get: function () { return registration_js_1.AppUpdateEvent; } });
22
22
  Object.defineProperty(exports, "AppUninstallEvent", { enumerable: true, get: function () { return registration_js_1.AppUninstallEvent; } });
23
+ Object.defineProperty(exports, "BeforeRegistrationEvent", { enumerable: true, get: function () { return registration_js_1.BeforeRegistrationEvent; } });
23
24
  //# sourceMappingURL=mod.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"mod.js","sourceRoot":"","sources":["../../src/mod.ts"],"names":[],"mappings":";;;AAAA,mCAAqC;AAA5B,mGAAA,SAAS,OAAA;AAClB,iDAAqE;AAA5D,uHAAA,sBAAsB,OAAA;AAAE,2GAAA,UAAU,OAAA;AAE3C,mDAK0B;AAJzB,4GAAA,UAAU,OAAA;AACV,oHAAA,kBAAkB,OAAA;AAClB,+HAAA,6BAA6B,OAAA;AAC7B,wHAAA,sBAAsB,OAAA;AAEvB,6DAAgD;AAAvC,8GAAA,OAAO,OAAA;AAChB,qDAO2B;AAN1B,qHAAA,kBAAkB,OAAA;AAClB,kHAAA,eAAe,OAAA;AACf,mHAAA,gBAAgB,OAAA;AAChB,qHAAA,kBAAkB,OAAA;AAClB,iHAAA,cAAc,OAAA;AACd,oHAAA,iBAAiB,OAAA","sourcesContent":["export { AppServer } from \"./app.js\";\nexport { InMemoryShopRepository, SimpleShop } from \"./repository.js\";\nexport type { ShopInterface, ShopRepositoryInterface } from \"./repository.js\";\nexport {\n\tHttpClient,\n\tHttpClientResponse,\n\tApiClientAuthenticationFailed,\n\tApiClientRequestFailed,\n} from \"./http-client.js\";\nexport { Context } from \"./context-resolver.js\";\nexport {\n\tShopAuthorizeEvent,\n\tAppInstallEvent,\n\tAppActivateEvent,\n\tAppDeactivateEvent,\n\tAppUpdateEvent,\n\tAppUninstallEvent,\n} from \"./registration.js\";\n"]}
1
+ {"version":3,"file":"mod.js","sourceRoot":"","sources":["../../src/mod.ts"],"names":[],"mappings":";;;AAAA,mCAAqC;AAA5B,mGAAA,SAAS,OAAA;AAClB,iDAAqE;AAA5D,uHAAA,sBAAsB,OAAA;AAAE,2GAAA,UAAU,OAAA;AAE3C,mDAK0B;AAJzB,4GAAA,UAAU,OAAA;AACV,oHAAA,kBAAkB,OAAA;AAClB,+HAAA,6BAA6B,OAAA;AAC7B,wHAAA,sBAAsB,OAAA;AAEvB,6DAAgD;AAAvC,8GAAA,OAAO,OAAA;AAChB,qDAQ2B;AAP1B,qHAAA,kBAAkB,OAAA;AAClB,kHAAA,eAAe,OAAA;AACf,mHAAA,gBAAgB,OAAA;AAChB,qHAAA,kBAAkB,OAAA;AAClB,iHAAA,cAAc,OAAA;AACd,oHAAA,iBAAiB,OAAA;AACjB,0HAAA,uBAAuB,OAAA","sourcesContent":["export { AppServer } from \"./app.js\";\nexport { InMemoryShopRepository, SimpleShop } from \"./repository.js\";\nexport type { ShopInterface, ShopRepositoryInterface } from \"./repository.js\";\nexport {\n\tHttpClient,\n\tHttpClientResponse,\n\tApiClientAuthenticationFailed,\n\tApiClientRequestFailed,\n} from \"./http-client.js\";\nexport { Context } from \"./context-resolver.js\";\nexport {\n\tShopAuthorizeEvent,\n\tAppInstallEvent,\n\tAppActivateEvent,\n\tAppDeactivateEvent,\n\tAppUpdateEvent,\n\tAppUninstallEvent,\n\tBeforeRegistrationEvent,\n} from \"./registration.js\";\n"]}
@@ -164,8 +164,8 @@ function randomString(length = 120) {
164
164
  return result;
165
165
  }
166
166
  class InvalidRequestResponse extends Response {
167
- constructor(message, status = 401) {
168
- super(JSON.stringify({ message }), {
167
+ constructor(error, status = 401) {
168
+ super(JSON.stringify({ error }), {
169
169
  status,
170
170
  headers: {
171
171
  "content-type": "application/json",
@@ -1 +1 @@
1
- {"version":3,"file":"registration.js","sourceRoot":"","sources":["../../src/registration.ts"],"names":[],"mappings":";;;AAwOA,oCAUC;AA/OD,MAAa,YAAY;IACJ;IAApB,YAAoB,GAAoB;QAApB,QAAG,GAAH,GAAG,CAAiB;IAAG,CAAC;IAE5C;;;;OAIG;IACI,KAAK,CAAC,SAAS,CAAC,GAAY;QAClC,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,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YAAO,GAAP,OAAO,CAAQ;IACpB,CAAC;IAEG,kBAAkB,CAAC,MAAc;QACvC,IAAI,CAAC,kBAAkB,GAAG,MAAM,CAAC;IAClC,CAAC;IAED,IAAW,MAAM;QAChB,OAAO,IAAI,CAAC,kBAAkB,CAAC;IAChC,CAAC;CACD;AAhBD,0DAgBC","sourcesContent":["import type { AppServer } from \"./app.js\";\nimport type { ShopInterface } from \"./repository.js\";\n\nexport class Registration<Shop extends ShopInterface = ShopInterface> {\n\tconstructor(private app: AppServer<Shop>) {}\n\n\t/**\n\t * This method checks the request for the handshake with the Shopware Shop.\n\t * if it's valid a Shop will be created, and a proof will be responded with a confirmation url.\n\t * then the Shop will call the confirmation url, and this should be handled by the authorizeCallback method to finish the handshake.\n\t */\n\tpublic async authorize(req: Request): Promise<Response> {\n\t\tconst url = new URL(req.url);\n\n\t\tif (\n\t\t\t!url.searchParams.has(\"shop-url\") ||\n\t\t\t!req.headers.has(\"shopware-app-signature\") ||\n\t\t\t!url.searchParams.has(\"shop-id\") ||\n\t\t\t!url.searchParams.has(\"timestamp\")\n\t\t) {\n\t\t\treturn new InvalidRequestResponse(\"Invalid Request\", 400);\n\t\t}\n\n\t\tconst shopId = url.searchParams.get(\"shop-id\") as string;\n\t\tconst shopUrl = url.searchParams.get(\"shop-url\") as string;\n\t\tconst timestamp = url.searchParams.get(\"timestamp\") as string;\n\n\t\tconst beforeRegistrationEvent = new BeforeRegistrationEvent(req, shopId, shopUrl);\n\t\tthis.app.hooks.publish('onBeforeRegistrationEvent', beforeRegistrationEvent);\n\n\t\tif (beforeRegistrationEvent.reason) {\n\t\t\treturn new InvalidRequestResponse(beforeRegistrationEvent.reason, 400);\n\t\t}\n\n\t\tconst v = await this.app.signer.verify(\n\t\t\treq.headers.get(\"shopware-app-signature\") as string,\n\t\t\t`shop-id=${shopId}&shop-url=${shopUrl}&timestamp=${timestamp}`,\n\t\t\tthis.app.cfg.appSecret,\n\t\t);\n\n\t\tif (!v) {\n\t\t\treturn new InvalidRequestResponse(\"Cannot validate app signature\");\n\t\t}\n\n\t\tconst shopSecret = randomString();\n\t\tconst sanitizedShopUrl = shopUrl\n\t\t\t.replace(/([^:])(\\/\\/+)/g, \"$1/\")\n\t\t\t.replace(/\\/+$/, \"\");\n\n\t\tawait this.app.repository.createShop(shopId, sanitizedShopUrl, shopSecret);\n\n\t\treturn new Response(\n\t\t\tJSON.stringify({\n\t\t\t\tproof: await this.app.signer.sign(\n\t\t\t\t\tshopId + shopUrl + this.app.cfg.appName,\n\t\t\t\t\tthis.app.cfg.appSecret,\n\t\t\t\t),\n\t\t\t\tsecret: shopSecret,\n\t\t\t\tconfirmation_url: this.app.cfg.authorizeCallbackUrl,\n\t\t\t}),\n\t\t\t{\n\t\t\t\theaders: {\n\t\t\t\t\t\"content-type\": \"application/json\",\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\t}\n\n\t/**\n\t * This method is called by the Shopware Shop to confirm the handshake.\n\t * It will update the shop with the given oauth2 credentials.\n\t */\n\tpublic async authorizeCallback(req: Request): Promise<Response> {\n\t\tconst bodyContent = await req.text();\n\n\t\tconst body = JSON.parse(bodyContent);\n\n\t\tif (\n\t\t\ttypeof body.shopId !== \"string\" ||\n\t\t\ttypeof body.apiKey !== \"string\" ||\n\t\t\ttypeof body.secretKey !== \"string\" ||\n\t\t\t!req.headers.has(\"shopware-shop-signature\")\n\t\t) {\n\t\t\treturn new InvalidRequestResponse(\"Invalid Request\", 400);\n\t\t}\n\n\t\tconst shop = await this.app.repository.getShopById(body.shopId as string);\n\n\t\tif (shop === null) {\n\t\t\treturn new InvalidRequestResponse(\"Invalid shop given\");\n\t\t}\n\n\t\tconst v = await this.app.signer.verify(\n\t\t\treq.headers.get(\"shopware-shop-signature\") as string,\n\t\t\tbodyContent,\n\t\t\tshop.getShopSecret(),\n\t\t);\n\n\t\tif (!v) {\n\t\t\t// Shop has failed the verification. Delete it from our DB.\n\t\t\tawait this.app.repository.deleteShop(shop.getShopId());\n\n\t\t\treturn new InvalidRequestResponse(\"Cannot validate app signature\");\n\t\t}\n\n\t\tshop.setShopCredentials(body.apiKey, body.secretKey);\n\n\t\tconst event = new ShopAuthorizeEvent(req, shop);\n\t\tawait this.app.hooks.publish(\"onAuthorize\", event);\n\n\t\tif (event.reason) {\n\t\t\tawait this.app.repository.deleteShop(shop.getShopId());\n\n\t\t\treturn new InvalidRequestResponse(event.reason, 403);\n\t\t}\n\n\t\tawait this.app.repository.updateShop(shop);\n\n\t\treturn new Response(null, { status: 204 });\n\t}\n\n\t/**\n\t * This method should be called by Shopware Shop to set the shop active.\n\t *\n\t * <webhooks>\n\t * <webhook name=\"appActivate\" url=\"http://localhost:3000/app/activate\" event=\"app.activated\"/>\n\t * </webhooks>\n\t */\n\tpublic async activate(req: Request): Promise<Response> {\n\t\tconst ctx = await this.app.contextResolver.fromAPI(req);\n\n\t\tconst event = new AppActivateEvent(req, ctx.shop);\n\t\tawait this.app.hooks.publish(\"onAppActivate\", event);\n\n\t\tctx.shop.setShopActive(true);\n\n\t\tawait this.app.repository.updateShop(ctx.shop);\n\n\t\treturn new Response(null, { status: 204 });\n\t}\n\n\t/**\n\t * This method should be called by Shopware when the app was installed.\n\t *\n\t * <webhooks>\n\t * <webhook name=\"appInstall\" url=\"http://localhost:3000/app/install\" event=\"app.installed\"/>\n\t * </webhooks>\n\t */\n\tpublic async install(req: Request): Promise<Response> {\n\t\tconst ctx = await this.app.contextResolver.fromAPI<{\n\t\t\tdata: { payload: { appVersion: string } };\n\t\t}>(req);\n\n\t\tconst event = new AppInstallEvent(\n\t\t\treq,\n\t\t\tctx.shop,\n\t\t\tctx.payload?.data?.payload?.appVersion ?? null,\n\t\t);\n\t\tawait this.app.hooks.publish(\"onAppInstall\", event);\n\n\t\treturn new Response(null, { status: 204 });\n\t}\n\n\t/**\n\t * This method should be called by Shopware Shop to set the shop in-active.\n\t *\n\t * <webhooks>\n\t * <webhook name=\"appDeactivated\" url=\"http://localhost:3000/app/deactivated\" event=\"app.deactivated\"/>\n\t * </webhooks>\n\t */\n\tpublic async deactivate(req: Request): Promise<Response> {\n\t\tconst ctx = await this.app.contextResolver.fromAPI(req);\n\n\t\tconst event = new AppDeactivateEvent(req, ctx.shop);\n\t\tawait this.app.hooks.publish(\"onAppDeactivate\", event);\n\n\t\tctx.shop.setShopActive(false);\n\n\t\tawait this.app.repository.updateShop(ctx.shop);\n\n\t\treturn new Response(null, { status: 204 });\n\t}\n\n\t/**\n\t * This method should be called by Shopware when the app was updated.\n\t *\n\t * <webhooks>\n\t * <webhook name=\"appUpdated\" url=\"http://localhost:3000/app/update\" event=\"app.updated\"/>\n\t * </webhooks>\n\t */\n\tpublic async update(req: Request): Promise<Response> {\n\t\tconst ctx = await this.app.contextResolver.fromAPI<{\n\t\t\tdata: { payload: { appVersion: string } };\n\t\t}>(req);\n\n\t\tconst event = new AppUpdateEvent(\n\t\t\treq,\n\t\t\tctx.shop,\n\t\t\tctx.payload?.data?.payload?.appVersion ?? null,\n\t\t);\n\t\tawait this.app.hooks.publish(\"onAppUpdate\", event);\n\n\t\treturn new Response(null, { status: 204 });\n\t}\n\n\t/**\n\t * This method should be called by Shopware Shop to delete the app.\n\t *\n\t * <webhooks>\n\t * <webhook name=\"appDelete\" url=\"http://localhost:3000/app/delete\" event=\"app.deleted\"/>\n\t * </webhooks>\n\t */\n\tpublic async delete(req: Request): Promise<Response> {\n\t\tconst ctx = await this.app.contextResolver.fromAPI<{\n\t\t\tdata: { payload: { keepUserData?: boolean } };\n\t\t}>(req);\n\n\t\tconst event = new AppUninstallEvent(\n\t\t\treq,\n\t\t\tctx.shop,\n\t\t\tctx.payload?.data?.payload?.keepUserData ?? null,\n\t\t);\n\t\tawait this.app.hooks.publish(\"onAppUninstall\", event);\n\n\t\tif (event.keepUserData === false) {\n\t\t\tawait this.app.repository.deleteShop(ctx.shop.getShopId());\n\t\t}\n\n\t\treturn new Response(null, { status: 204 });\n\t}\n}\n\nexport function randomString(length = 120) {\n\tlet result = \"\";\n\tconst characters =\n\t\t\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\";\n\tconst charactersLength = characters.length;\n\tfor (let i = 0; i < length; i++) {\n\t\tresult += characters.charAt(Math.floor(Math.random() * charactersLength));\n\t}\n\n\treturn result;\n}\n\nclass InvalidRequestResponse extends Response {\n\tconstructor(message: string, status = 401) {\n\t\tsuper(JSON.stringify({ message }), {\n\t\t\tstatus,\n\t\t\theaders: {\n\t\t\t\t\"content-type\": \"application/json\",\n\t\t\t},\n\t\t});\n\t}\n}\n\nexport class ShopAuthorizeEvent<Shop extends ShopInterface = ShopInterface> {\n\tprivate reject: string | null = null;\n\n\tconstructor(\n\t\tpublic request: Request,\n\t\tpublic shop: Shop,\n\t) {}\n\n\tpublic rejectRegistration(reason: string) {\n\t\tthis.reject = reason;\n\t}\n\n\tpublic get reason() {\n\t\treturn this.reject;\n\t}\n}\n\nexport class AppInstallEvent<Shop extends ShopInterface = ShopInterface> {\n\tconstructor(\n\t\tpublic request: Request,\n\t\tpublic shop: Shop,\n\t\tpublic appVersion: string | null = null,\n\t) {}\n}\n\nexport class AppActivateEvent<Shop extends ShopInterface = ShopInterface> {\n\tconstructor(\n\t\tpublic request: Request,\n\t\tpublic shop: Shop,\n\t) {}\n}\n\nexport class AppDeactivateEvent<Shop extends ShopInterface = ShopInterface> {\n\tconstructor(\n\t\tpublic request: Request,\n\t\tpublic shop: Shop,\n\t) {}\n}\n\nexport class AppUpdateEvent<Shop extends ShopInterface = ShopInterface> {\n\tconstructor(\n\t\tpublic request: Request,\n\t\tpublic shop: Shop,\n\t\tpublic appVersion: string | null = null,\n\t) {}\n}\n\nexport class AppUninstallEvent<Shop extends ShopInterface = ShopInterface> {\n\tconstructor(\n\t\tpublic request: Request,\n\t\tpublic shop: Shop,\n\t\tpublic keepUserData: boolean | null = null,\n\t) {}\n}\n\nexport class BeforeRegistrationEvent<Shop extends ShopInterface = ShopInterface> {\n\tprivate cancellationReason: string | null = null;\n\t\n\tconstructor(\n\t\tpublic request: Request,\n\t\tpublic shopId: string,\n\t\tpublic shopUrl: string,\n\t) {}\n\n\tpublic rejectRegistration(reason: string) {\n\t\tthis.cancellationReason = reason;\n\t}\n\n\tpublic get reason() {\n\t\treturn this.cancellationReason;\n\t}\n}\n"]}
1
+ {"version":3,"file":"registration.js","sourceRoot":"","sources":["../../src/registration.ts"],"names":[],"mappings":";;;AAwOA,oCAUC;AA/OD,MAAa,YAAY;IACJ;IAApB,YAAoB,GAAoB;QAApB,QAAG,GAAH,GAAG,CAAiB;IAAG,CAAC;IAE5C;;;;OAIG;IACI,KAAK,CAAC,SAAS,CAAC,GAAY;QAClC,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAE7B,IACC,CAAC,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,UAAU,CAAC;YACjC,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,wBAAwB,CAAC;YAC1C,CAAC,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,SAAS,CAAC;YAChC,CAAC,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,WAAW,CAAC,EACjC,CAAC;YACF,OAAO,IAAI,sBAAsB,CAAC,iBAAiB,EAAE,GAAG,CAAC,CAAC;QAC3D,CAAC;QAED,MAAM,MAAM,GAAG,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,SAAS,CAAW,CAAC;QACzD,MAAM,OAAO,GAAG,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,UAAU,CAAW,CAAC;QAC3D,MAAM,SAAS,GAAG,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,WAAW,CAAW,CAAC;QAE9D,MAAM,uBAAuB,GAAG,IAAI,uBAAuB,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;QAClF,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,2BAA2B,EAAE,uBAAuB,CAAC,CAAC;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;QACV,eAAU,GAAV,UAAU,CAAsB;IACrC,CAAC;CACJ;AAND,wCAMC;AAED,MAAa,iBAAiB;IAErB;IACA;IACA;IAHR,YACQ,OAAgB,EAChB,IAAU,EACV,eAA+B,IAAI;QAFnC,YAAO,GAAP,OAAO,CAAS;QAChB,SAAI,GAAJ,IAAI,CAAM;QACV,iBAAY,GAAZ,YAAY,CAAuB;IACxC,CAAC;CACJ;AAND,8CAMC;AAED,MAAa,uBAAuB;IAI3B;IACA;IACA;IALA,kBAAkB,GAAkB,IAAI,CAAC;IAEjD,YACQ,OAAgB,EAChB,MAAc,EACd,OAAe;QAFf,YAAO,GAAP,OAAO,CAAS;QAChB,WAAM,GAAN,MAAM,CAAQ;QACd,YAAO,GAAP,OAAO,CAAQ;IACpB,CAAC;IAEG,kBAAkB,CAAC,MAAc;QACvC,IAAI,CAAC,kBAAkB,GAAG,MAAM,CAAC;IAClC,CAAC;IAED,IAAW,MAAM;QAChB,OAAO,IAAI,CAAC,kBAAkB,CAAC;IAChC,CAAC;CACD;AAhBD,0DAgBC","sourcesContent":["import type { AppServer } from \"./app.js\";\nimport type { ShopInterface } from \"./repository.js\";\n\nexport class Registration<Shop extends ShopInterface = ShopInterface> {\n\tconstructor(private app: AppServer<Shop>) {}\n\n\t/**\n\t * This method checks the request for the handshake with the Shopware Shop.\n\t * if it's valid a Shop will be created, and a proof will be responded with a confirmation url.\n\t * then the Shop will call the confirmation url, and this should be handled by the authorizeCallback method to finish the handshake.\n\t */\n\tpublic async authorize(req: Request): Promise<Response> {\n\t\tconst url = new URL(req.url);\n\n\t\tif (\n\t\t\t!url.searchParams.has(\"shop-url\") ||\n\t\t\t!req.headers.has(\"shopware-app-signature\") ||\n\t\t\t!url.searchParams.has(\"shop-id\") ||\n\t\t\t!url.searchParams.has(\"timestamp\")\n\t\t) {\n\t\t\treturn new InvalidRequestResponse(\"Invalid Request\", 400);\n\t\t}\n\n\t\tconst shopId = url.searchParams.get(\"shop-id\") as string;\n\t\tconst shopUrl = url.searchParams.get(\"shop-url\") as string;\n\t\tconst timestamp = url.searchParams.get(\"timestamp\") as string;\n\n\t\tconst beforeRegistrationEvent = new BeforeRegistrationEvent(req, shopId, shopUrl);\n\t\tthis.app.hooks.publish('onBeforeRegistrationEvent', beforeRegistrationEvent);\n\n\t\tif (beforeRegistrationEvent.reason) {\n\t\t\treturn new InvalidRequestResponse(beforeRegistrationEvent.reason, 400);\n\t\t}\n\n\t\tconst v = await this.app.signer.verify(\n\t\t\treq.headers.get(\"shopware-app-signature\") as string,\n\t\t\t`shop-id=${shopId}&shop-url=${shopUrl}&timestamp=${timestamp}`,\n\t\t\tthis.app.cfg.appSecret,\n\t\t);\n\n\t\tif (!v) {\n\t\t\treturn new InvalidRequestResponse(\"Cannot validate app signature\");\n\t\t}\n\n\t\tconst shopSecret = randomString();\n\t\tconst sanitizedShopUrl = shopUrl\n\t\t\t.replace(/([^:])(\\/\\/+)/g, \"$1/\")\n\t\t\t.replace(/\\/+$/, \"\");\n\n\t\tawait this.app.repository.createShop(shopId, sanitizedShopUrl, shopSecret);\n\n\t\treturn new Response(\n\t\t\tJSON.stringify({\n\t\t\t\tproof: await this.app.signer.sign(\n\t\t\t\t\tshopId + shopUrl + this.app.cfg.appName,\n\t\t\t\t\tthis.app.cfg.appSecret,\n\t\t\t\t),\n\t\t\t\tsecret: shopSecret,\n\t\t\t\tconfirmation_url: this.app.cfg.authorizeCallbackUrl,\n\t\t\t}),\n\t\t\t{\n\t\t\t\theaders: {\n\t\t\t\t\t\"content-type\": \"application/json\",\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\t}\n\n\t/**\n\t * This method is called by the Shopware Shop to confirm the handshake.\n\t * It will update the shop with the given oauth2 credentials.\n\t */\n\tpublic async authorizeCallback(req: Request): Promise<Response> {\n\t\tconst bodyContent = await req.text();\n\n\t\tconst body = JSON.parse(bodyContent);\n\n\t\tif (\n\t\t\ttypeof body.shopId !== \"string\" ||\n\t\t\ttypeof body.apiKey !== \"string\" ||\n\t\t\ttypeof body.secretKey !== \"string\" ||\n\t\t\t!req.headers.has(\"shopware-shop-signature\")\n\t\t) {\n\t\t\treturn new InvalidRequestResponse(\"Invalid Request\", 400);\n\t\t}\n\n\t\tconst shop = await this.app.repository.getShopById(body.shopId as string);\n\n\t\tif (shop === null) {\n\t\t\treturn new InvalidRequestResponse(\"Invalid shop given\");\n\t\t}\n\n\t\tconst v = await this.app.signer.verify(\n\t\t\treq.headers.get(\"shopware-shop-signature\") as string,\n\t\t\tbodyContent,\n\t\t\tshop.getShopSecret(),\n\t\t);\n\n\t\tif (!v) {\n\t\t\t// Shop has failed the verification. Delete it from our DB.\n\t\t\tawait this.app.repository.deleteShop(shop.getShopId());\n\n\t\t\treturn new InvalidRequestResponse(\"Cannot validate app signature\");\n\t\t}\n\n\t\tshop.setShopCredentials(body.apiKey, body.secretKey);\n\n\t\tconst event = new ShopAuthorizeEvent(req, shop);\n\t\tawait this.app.hooks.publish(\"onAuthorize\", event);\n\n\t\tif (event.reason) {\n\t\t\tawait this.app.repository.deleteShop(shop.getShopId());\n\n\t\t\treturn new InvalidRequestResponse(event.reason, 403);\n\t\t}\n\n\t\tawait this.app.repository.updateShop(shop);\n\n\t\treturn new Response(null, { status: 204 });\n\t}\n\n\t/**\n\t * This method should be called by Shopware Shop to set the shop active.\n\t *\n\t * <webhooks>\n\t * <webhook name=\"appActivate\" url=\"http://localhost:3000/app/activate\" event=\"app.activated\"/>\n\t * </webhooks>\n\t */\n\tpublic async activate(req: Request): Promise<Response> {\n\t\tconst ctx = await this.app.contextResolver.fromAPI(req);\n\n\t\tconst event = new AppActivateEvent(req, ctx.shop);\n\t\tawait this.app.hooks.publish(\"onAppActivate\", event);\n\n\t\tctx.shop.setShopActive(true);\n\n\t\tawait this.app.repository.updateShop(ctx.shop);\n\n\t\treturn new Response(null, { status: 204 });\n\t}\n\n\t/**\n\t * This method should be called by Shopware when the app was installed.\n\t *\n\t * <webhooks>\n\t * <webhook name=\"appInstall\" url=\"http://localhost:3000/app/install\" event=\"app.installed\"/>\n\t * </webhooks>\n\t */\n\tpublic async install(req: Request): Promise<Response> {\n\t\tconst ctx = await this.app.contextResolver.fromAPI<{\n\t\t\tdata: { payload: { appVersion: string } };\n\t\t}>(req);\n\n\t\tconst event = new AppInstallEvent(\n\t\t\treq,\n\t\t\tctx.shop,\n\t\t\tctx.payload?.data?.payload?.appVersion ?? null,\n\t\t);\n\t\tawait this.app.hooks.publish(\"onAppInstall\", event);\n\n\t\treturn new Response(null, { status: 204 });\n\t}\n\n\t/**\n\t * This method should be called by Shopware Shop to set the shop in-active.\n\t *\n\t * <webhooks>\n\t * <webhook name=\"appDeactivated\" url=\"http://localhost:3000/app/deactivated\" event=\"app.deactivated\"/>\n\t * </webhooks>\n\t */\n\tpublic async deactivate(req: Request): Promise<Response> {\n\t\tconst ctx = await this.app.contextResolver.fromAPI(req);\n\n\t\tconst event = new AppDeactivateEvent(req, ctx.shop);\n\t\tawait this.app.hooks.publish(\"onAppDeactivate\", event);\n\n\t\tctx.shop.setShopActive(false);\n\n\t\tawait this.app.repository.updateShop(ctx.shop);\n\n\t\treturn new Response(null, { status: 204 });\n\t}\n\n\t/**\n\t * This method should be called by Shopware when the app was updated.\n\t *\n\t * <webhooks>\n\t * <webhook name=\"appUpdated\" url=\"http://localhost:3000/app/update\" event=\"app.updated\"/>\n\t * </webhooks>\n\t */\n\tpublic async update(req: Request): Promise<Response> {\n\t\tconst ctx = await this.app.contextResolver.fromAPI<{\n\t\t\tdata: { payload: { appVersion: string } };\n\t\t}>(req);\n\n\t\tconst event = new AppUpdateEvent(\n\t\t\treq,\n\t\t\tctx.shop,\n\t\t\tctx.payload?.data?.payload?.appVersion ?? null,\n\t\t);\n\t\tawait this.app.hooks.publish(\"onAppUpdate\", event);\n\n\t\treturn new Response(null, { status: 204 });\n\t}\n\n\t/**\n\t * This method should be called by Shopware Shop to delete the app.\n\t *\n\t * <webhooks>\n\t * <webhook name=\"appDelete\" url=\"http://localhost:3000/app/delete\" event=\"app.deleted\"/>\n\t * </webhooks>\n\t */\n\tpublic async delete(req: Request): Promise<Response> {\n\t\tconst ctx = await this.app.contextResolver.fromAPI<{\n\t\t\tdata: { payload: { keepUserData?: boolean } };\n\t\t}>(req);\n\n\t\tconst event = new AppUninstallEvent(\n\t\t\treq,\n\t\t\tctx.shop,\n\t\t\tctx.payload?.data?.payload?.keepUserData ?? null,\n\t\t);\n\t\tawait this.app.hooks.publish(\"onAppUninstall\", event);\n\n\t\tif (event.keepUserData === false) {\n\t\t\tawait this.app.repository.deleteShop(ctx.shop.getShopId());\n\t\t}\n\n\t\treturn new Response(null, { status: 204 });\n\t}\n}\n\nexport function randomString(length = 120) {\n\tlet result = \"\";\n\tconst characters =\n\t\t\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\";\n\tconst charactersLength = characters.length;\n\tfor (let i = 0; i < length; i++) {\n\t\tresult += characters.charAt(Math.floor(Math.random() * charactersLength));\n\t}\n\n\treturn result;\n}\n\nclass InvalidRequestResponse extends Response {\n\tconstructor(error: string, status = 401) {\n\t\tsuper(JSON.stringify({ error }), {\n\t\t\tstatus,\n\t\t\theaders: {\n\t\t\t\t\"content-type\": \"application/json\",\n\t\t\t},\n\t\t});\n\t}\n}\n\nexport class ShopAuthorizeEvent<Shop extends ShopInterface = ShopInterface> {\n\tprivate reject: string | null = null;\n\n\tconstructor(\n\t\tpublic request: Request,\n\t\tpublic shop: Shop,\n\t) {}\n\n\tpublic rejectRegistration(reason: string) {\n\t\tthis.reject = reason;\n\t}\n\n\tpublic get reason() {\n\t\treturn this.reject;\n\t}\n}\n\nexport class AppInstallEvent<Shop extends ShopInterface = ShopInterface> {\n\tconstructor(\n\t\tpublic request: Request,\n\t\tpublic shop: Shop,\n\t\tpublic appVersion: string | null = null,\n\t) {}\n}\n\nexport class AppActivateEvent<Shop extends ShopInterface = ShopInterface> {\n\tconstructor(\n\t\tpublic request: Request,\n\t\tpublic shop: Shop,\n\t) {}\n}\n\nexport class AppDeactivateEvent<Shop extends ShopInterface = ShopInterface> {\n\tconstructor(\n\t\tpublic request: Request,\n\t\tpublic shop: Shop,\n\t) {}\n}\n\nexport class AppUpdateEvent<Shop extends ShopInterface = ShopInterface> {\n\tconstructor(\n\t\tpublic request: Request,\n\t\tpublic shop: Shop,\n\t\tpublic appVersion: string | null = null,\n\t) {}\n}\n\nexport class AppUninstallEvent<Shop extends ShopInterface = ShopInterface> {\n\tconstructor(\n\t\tpublic request: Request,\n\t\tpublic shop: Shop,\n\t\tpublic keepUserData: boolean | null = null,\n\t) {}\n}\n\nexport class BeforeRegistrationEvent<Shop extends ShopInterface = ShopInterface> {\n\tprivate cancellationReason: string | null = null;\n\t\n\tconstructor(\n\t\tpublic request: Request,\n\t\tpublic shopId: string,\n\t\tpublic shopUrl: string,\n\t) {}\n\n\tpublic rejectRegistration(reason: string) {\n\t\tthis.cancellationReason = reason;\n\t}\n\n\tpublic get reason() {\n\t\treturn this.cancellationReason;\n\t}\n}\n"]}
package/dist/esm/mod.d.ts CHANGED
@@ -3,5 +3,5 @@ export { InMemoryShopRepository, SimpleShop } from "./repository.js";
3
3
  export type { ShopInterface, ShopRepositoryInterface } from "./repository.js";
4
4
  export { HttpClient, HttpClientResponse, ApiClientAuthenticationFailed, ApiClientRequestFailed, } from "./http-client.js";
5
5
  export { Context } from "./context-resolver.js";
6
- export { ShopAuthorizeEvent, AppInstallEvent, AppActivateEvent, AppDeactivateEvent, AppUpdateEvent, AppUninstallEvent, } from "./registration.js";
6
+ export { ShopAuthorizeEvent, AppInstallEvent, AppActivateEvent, AppDeactivateEvent, AppUpdateEvent, AppUninstallEvent, BeforeRegistrationEvent, } from "./registration.js";
7
7
  //# sourceMappingURL=mod.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AACrC,OAAO,EAAE,sBAAsB,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AACrE,YAAY,EAAE,aAAa,EAAE,uBAAuB,EAAE,MAAM,iBAAiB,CAAC;AAC9E,OAAO,EACN,UAAU,EACV,kBAAkB,EAClB,6BAA6B,EAC7B,sBAAsB,GACtB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAChD,OAAO,EACN,kBAAkB,EAClB,eAAe,EACf,gBAAgB,EAChB,kBAAkB,EAClB,cAAc,EACd,iBAAiB,GACjB,MAAM,mBAAmB,CAAC"}
1
+ {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AACrC,OAAO,EAAE,sBAAsB,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AACrE,YAAY,EAAE,aAAa,EAAE,uBAAuB,EAAE,MAAM,iBAAiB,CAAC;AAC9E,OAAO,EACN,UAAU,EACV,kBAAkB,EAClB,6BAA6B,EAC7B,sBAAsB,GACtB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAChD,OAAO,EACN,kBAAkB,EAClB,eAAe,EACf,gBAAgB,EAChB,kBAAkB,EAClB,cAAc,EACd,iBAAiB,EACjB,uBAAuB,GACvB,MAAM,mBAAmB,CAAC"}
package/dist/esm/mod.js CHANGED
@@ -2,5 +2,5 @@ export { AppServer } from "./app.js";
2
2
  export { InMemoryShopRepository, SimpleShop } from "./repository.js";
3
3
  export { HttpClient, HttpClientResponse, ApiClientAuthenticationFailed, ApiClientRequestFailed, } from "./http-client.js";
4
4
  export { Context } from "./context-resolver.js";
5
- export { ShopAuthorizeEvent, AppInstallEvent, AppActivateEvent, AppDeactivateEvent, AppUpdateEvent, AppUninstallEvent, } from "./registration.js";
5
+ export { ShopAuthorizeEvent, AppInstallEvent, AppActivateEvent, AppDeactivateEvent, AppUpdateEvent, AppUninstallEvent, BeforeRegistrationEvent, } from "./registration.js";
6
6
  //# sourceMappingURL=mod.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"mod.js","sourceRoot":"","sources":["../../src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AACrC,OAAO,EAAE,sBAAsB,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAErE,OAAO,EACN,UAAU,EACV,kBAAkB,EAClB,6BAA6B,EAC7B,sBAAsB,GACtB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAChD,OAAO,EACN,kBAAkB,EAClB,eAAe,EACf,gBAAgB,EAChB,kBAAkB,EAClB,cAAc,EACd,iBAAiB,GACjB,MAAM,mBAAmB,CAAC","sourcesContent":["export { AppServer } from \"./app.js\";\nexport { InMemoryShopRepository, SimpleShop } from \"./repository.js\";\nexport type { ShopInterface, ShopRepositoryInterface } from \"./repository.js\";\nexport {\n\tHttpClient,\n\tHttpClientResponse,\n\tApiClientAuthenticationFailed,\n\tApiClientRequestFailed,\n} from \"./http-client.js\";\nexport { Context } from \"./context-resolver.js\";\nexport {\n\tShopAuthorizeEvent,\n\tAppInstallEvent,\n\tAppActivateEvent,\n\tAppDeactivateEvent,\n\tAppUpdateEvent,\n\tAppUninstallEvent,\n} from \"./registration.js\";\n"]}
1
+ {"version":3,"file":"mod.js","sourceRoot":"","sources":["../../src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AACrC,OAAO,EAAE,sBAAsB,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAErE,OAAO,EACN,UAAU,EACV,kBAAkB,EAClB,6BAA6B,EAC7B,sBAAsB,GACtB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAChD,OAAO,EACN,kBAAkB,EAClB,eAAe,EACf,gBAAgB,EAChB,kBAAkB,EAClB,cAAc,EACd,iBAAiB,EACjB,uBAAuB,GACvB,MAAM,mBAAmB,CAAC","sourcesContent":["export { AppServer } from \"./app.js\";\nexport { InMemoryShopRepository, SimpleShop } from \"./repository.js\";\nexport type { ShopInterface, ShopRepositoryInterface } from \"./repository.js\";\nexport {\n\tHttpClient,\n\tHttpClientResponse,\n\tApiClientAuthenticationFailed,\n\tApiClientRequestFailed,\n} from \"./http-client.js\";\nexport { Context } from \"./context-resolver.js\";\nexport {\n\tShopAuthorizeEvent,\n\tAppInstallEvent,\n\tAppActivateEvent,\n\tAppDeactivateEvent,\n\tAppUpdateEvent,\n\tAppUninstallEvent,\n\tBeforeRegistrationEvent,\n} from \"./registration.js\";\n"]}
@@ -159,8 +159,8 @@ export function randomString(length = 120) {
159
159
  return result;
160
160
  }
161
161
  class InvalidRequestResponse extends Response {
162
- constructor(message, status = 401) {
163
- super(JSON.stringify({ message }), {
162
+ constructor(error, status = 401) {
163
+ super(JSON.stringify({ error }), {
164
164
  status,
165
165
  headers: {
166
166
  "content-type": "application/json",
@@ -1 +1 @@
1
- {"version":3,"file":"registration.js","sourceRoot":"","sources":["../../src/registration.ts"],"names":[],"mappings":"AAGA,MAAM,OAAO,YAAY;IACJ;IAApB,YAAoB,GAAoB;QAApB,QAAG,GAAH,GAAG,CAAiB;IAAG,CAAC;IAE5C;;;;OAIG;IACI,KAAK,CAAC,SAAS,CAAC,GAAY;QAClC,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAE7B,IACC,CAAC,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,UAAU,CAAC;YACjC,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,wBAAwB,CAAC;YAC1C,CAAC,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,SAAS,CAAC;YAChC,CAAC,GAAG,CAAC,YA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AAJ,IAAI,CAAM;QACV,eAAU,GAAV,UAAU,CAAsB;IACrC,CAAC;CACJ;AAED,MAAM,OAAO,gBAAgB;IAEpB;IACA;IAFR,YACQ,OAAgB,EAChB,IAAU;QADV,YAAO,GAAP,OAAO,CAAS;QAChB,SAAI,GAAJ,IAAI,CAAM;IACf,CAAC;CACJ;AAED,MAAM,OAAO,kBAAkB;IAEtB;IACA;IAFR,YACQ,OAAgB,EAChB,IAAU;QADV,YAAO,GAAP,OAAO,CAAS;QAChB,SAAI,GAAJ,IAAI,CAAM;IACf,CAAC;CACJ;AAED,MAAM,OAAO,cAAc;IAElB;IACA;IACA;IAHR,YACQ,OAAgB,EAChB,IAAU,EACV,aAA4B,IAAI;QAFhC,YAAO,GAAP,OAAO,CAAS;QAChB,SAAI,GAAJ,IAAI,CAAM;QACV,eAAU,GAAV,UAAU,CAAsB;IACrC,CAAC;CACJ;AAED,MAAM,OAAO,iBAAiB;IAErB;IACA;IACA;IAHR,YACQ,OAAgB,EAChB,IAAU,EACV,eAA+B,IAAI;QAFnC,YAAO,GAAP,OAAO,CAAS;QAChB,SAAI,GAAJ,IAAI,CAAM;QACV,iBAAY,GAAZ,YAAY,CAAuB;IACxC,CAAC;CACJ;AAED,MAAM,OAAO,uBAAuB;IAI3B;IACA;IACA;IALA,kBAAkB,GAAkB,IAAI,CAAC;IAEjD,YACQ,OAAgB,EAChB,MAAc,EACd,OAAe;QAFf,YAAO,GAAP,OAAO,CAAS;QAChB,WAAM,GAAN,MAAM,CAAQ;QACd,YAAO,GAAP,OAAO,CAAQ;IACpB,CAAC;IAEG,kBAAkB,CAAC,MAAc;QACvC,IAAI,CAAC,kBAAkB,GAAG,MAAM,CAAC;IAClC,CAAC;IAED,IAAW,MAAM;QAChB,OAAO,IAAI,CAAC,kBAAkB,CAAC;IAChC,CAAC;CACD","sourcesContent":["import type { AppServer } from \"./app.js\";\nimport type { ShopInterface } from \"./repository.js\";\n\nexport class Registration<Shop extends ShopInterface = ShopInterface> {\n\tconstructor(private app: AppServer<Shop>) {}\n\n\t/**\n\t * This method checks the request for the handshake with the Shopware Shop.\n\t * if it's valid a Shop will be created, and a proof will be responded with a confirmation url.\n\t * then the Shop will call the confirmation url, and this should be handled by the authorizeCallback method to finish the handshake.\n\t */\n\tpublic async authorize(req: Request): Promise<Response> {\n\t\tconst url = new URL(req.url);\n\n\t\tif (\n\t\t\t!url.searchParams.has(\"shop-url\") ||\n\t\t\t!req.headers.has(\"shopware-app-signature\") ||\n\t\t\t!url.searchParams.has(\"shop-id\") ||\n\t\t\t!url.searchParams.has(\"timestamp\")\n\t\t) {\n\t\t\treturn new InvalidRequestResponse(\"Invalid Request\", 400);\n\t\t}\n\n\t\tconst shopId = url.searchParams.get(\"shop-id\") as string;\n\t\tconst shopUrl = url.searchParams.get(\"shop-url\") as string;\n\t\tconst timestamp = url.searchParams.get(\"timestamp\") as string;\n\n\t\tconst beforeRegistrationEvent = new BeforeRegistrationEvent(req, shopId, shopUrl);\n\t\tthis.app.hooks.publish('onBeforeRegistrationEvent', beforeRegistrationEvent);\n\n\t\tif (beforeRegistrationEvent.reason) {\n\t\t\treturn new InvalidRequestResponse(beforeRegistrationEvent.reason, 400);\n\t\t}\n\n\t\tconst v = await this.app.signer.verify(\n\t\t\treq.headers.get(\"shopware-app-signature\") as string,\n\t\t\t`shop-id=${shopId}&shop-url=${shopUrl}&timestamp=${timestamp}`,\n\t\t\tthis.app.cfg.appSecret,\n\t\t);\n\n\t\tif (!v) {\n\t\t\treturn new InvalidRequestResponse(\"Cannot validate app signature\");\n\t\t}\n\n\t\tconst shopSecret = randomString();\n\t\tconst sanitizedShopUrl = shopUrl\n\t\t\t.replace(/([^:])(\\/\\/+)/g, \"$1/\")\n\t\t\t.replace(/\\/+$/, \"\");\n\n\t\tawait this.app.repository.createShop(shopId, sanitizedShopUrl, shopSecret);\n\n\t\treturn new Response(\n\t\t\tJSON.stringify({\n\t\t\t\tproof: await this.app.signer.sign(\n\t\t\t\t\tshopId + shopUrl + this.app.cfg.appName,\n\t\t\t\t\tthis.app.cfg.appSecret,\n\t\t\t\t),\n\t\t\t\tsecret: shopSecret,\n\t\t\t\tconfirmation_url: this.app.cfg.authorizeCallbackUrl,\n\t\t\t}),\n\t\t\t{\n\t\t\t\theaders: {\n\t\t\t\t\t\"content-type\": \"application/json\",\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\t}\n\n\t/**\n\t * This method is called by the Shopware Shop to confirm the handshake.\n\t * It will update the shop with the given oauth2 credentials.\n\t */\n\tpublic async authorizeCallback(req: Request): Promise<Response> {\n\t\tconst bodyContent = await req.text();\n\n\t\tconst body = JSON.parse(bodyContent);\n\n\t\tif (\n\t\t\ttypeof body.shopId !== \"string\" ||\n\t\t\ttypeof body.apiKey !== \"string\" ||\n\t\t\ttypeof body.secretKey !== \"string\" ||\n\t\t\t!req.headers.has(\"shopware-shop-signature\")\n\t\t) {\n\t\t\treturn new InvalidRequestResponse(\"Invalid Request\", 400);\n\t\t}\n\n\t\tconst shop = await this.app.repository.getShopById(body.shopId as string);\n\n\t\tif (shop === null) {\n\t\t\treturn new InvalidRequestResponse(\"Invalid shop given\");\n\t\t}\n\n\t\tconst v = await this.app.signer.verify(\n\t\t\treq.headers.get(\"shopware-shop-signature\") as string,\n\t\t\tbodyContent,\n\t\t\tshop.getShopSecret(),\n\t\t);\n\n\t\tif (!v) {\n\t\t\t// Shop has failed the verification. Delete it from our DB.\n\t\t\tawait this.app.repository.deleteShop(shop.getShopId());\n\n\t\t\treturn new InvalidRequestResponse(\"Cannot validate app signature\");\n\t\t}\n\n\t\tshop.setShopCredentials(body.apiKey, body.secretKey);\n\n\t\tconst event = new ShopAuthorizeEvent(req, shop);\n\t\tawait this.app.hooks.publish(\"onAuthorize\", event);\n\n\t\tif (event.reason) {\n\t\t\tawait this.app.repository.deleteShop(shop.getShopId());\n\n\t\t\treturn new InvalidRequestResponse(event.reason, 403);\n\t\t}\n\n\t\tawait this.app.repository.updateShop(shop);\n\n\t\treturn new Response(null, { status: 204 });\n\t}\n\n\t/**\n\t * This method should be called by Shopware Shop to set the shop active.\n\t *\n\t * <webhooks>\n\t * <webhook name=\"appActivate\" url=\"http://localhost:3000/app/activate\" event=\"app.activated\"/>\n\t * </webhooks>\n\t */\n\tpublic async activate(req: Request): Promise<Response> {\n\t\tconst ctx = await this.app.contextResolver.fromAPI(req);\n\n\t\tconst event = new AppActivateEvent(req, ctx.shop);\n\t\tawait this.app.hooks.publish(\"onAppActivate\", event);\n\n\t\tctx.shop.setShopActive(true);\n\n\t\tawait this.app.repository.updateShop(ctx.shop);\n\n\t\treturn new Response(null, { status: 204 });\n\t}\n\n\t/**\n\t * This method should be called by Shopware when the app was installed.\n\t *\n\t * <webhooks>\n\t * <webhook name=\"appInstall\" url=\"http://localhost:3000/app/install\" event=\"app.installed\"/>\n\t * </webhooks>\n\t */\n\tpublic async install(req: Request): Promise<Response> {\n\t\tconst ctx = await this.app.contextResolver.fromAPI<{\n\t\t\tdata: { payload: { appVersion: string } };\n\t\t}>(req);\n\n\t\tconst event = new AppInstallEvent(\n\t\t\treq,\n\t\t\tctx.shop,\n\t\t\tctx.payload?.data?.payload?.appVersion ?? null,\n\t\t);\n\t\tawait this.app.hooks.publish(\"onAppInstall\", event);\n\n\t\treturn new Response(null, { status: 204 });\n\t}\n\n\t/**\n\t * This method should be called by Shopware Shop to set the shop in-active.\n\t *\n\t * <webhooks>\n\t * <webhook name=\"appDeactivated\" url=\"http://localhost:3000/app/deactivated\" event=\"app.deactivated\"/>\n\t * </webhooks>\n\t */\n\tpublic async deactivate(req: Request): Promise<Response> {\n\t\tconst ctx = await this.app.contextResolver.fromAPI(req);\n\n\t\tconst event = new AppDeactivateEvent(req, ctx.shop);\n\t\tawait this.app.hooks.publish(\"onAppDeactivate\", event);\n\n\t\tctx.shop.setShopActive(false);\n\n\t\tawait this.app.repository.updateShop(ctx.shop);\n\n\t\treturn new Response(null, { status: 204 });\n\t}\n\n\t/**\n\t * This method should be called by Shopware when the app was updated.\n\t *\n\t * <webhooks>\n\t * <webhook name=\"appUpdated\" url=\"http://localhost:3000/app/update\" event=\"app.updated\"/>\n\t * </webhooks>\n\t */\n\tpublic async update(req: Request): Promise<Response> {\n\t\tconst ctx = await this.app.contextResolver.fromAPI<{\n\t\t\tdata: { payload: { appVersion: string } };\n\t\t}>(req);\n\n\t\tconst event = new AppUpdateEvent(\n\t\t\treq,\n\t\t\tctx.shop,\n\t\t\tctx.payload?.data?.payload?.appVersion ?? null,\n\t\t);\n\t\tawait this.app.hooks.publish(\"onAppUpdate\", event);\n\n\t\treturn new Response(null, { status: 204 });\n\t}\n\n\t/**\n\t * This method should be called by Shopware Shop to delete the app.\n\t *\n\t * <webhooks>\n\t * <webhook name=\"appDelete\" url=\"http://localhost:3000/app/delete\" event=\"app.deleted\"/>\n\t * </webhooks>\n\t */\n\tpublic async delete(req: Request): Promise<Response> {\n\t\tconst ctx = await this.app.contextResolver.fromAPI<{\n\t\t\tdata: { payload: { keepUserData?: boolean } };\n\t\t}>(req);\n\n\t\tconst event = new AppUninstallEvent(\n\t\t\treq,\n\t\t\tctx.shop,\n\t\t\tctx.payload?.data?.payload?.keepUserData ?? null,\n\t\t);\n\t\tawait this.app.hooks.publish(\"onAppUninstall\", event);\n\n\t\tif (event.keepUserData === false) {\n\t\t\tawait this.app.repository.deleteShop(ctx.shop.getShopId());\n\t\t}\n\n\t\treturn new Response(null, { status: 204 });\n\t}\n}\n\nexport function randomString(length = 120) {\n\tlet result = \"\";\n\tconst characters =\n\t\t\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\";\n\tconst charactersLength = characters.length;\n\tfor (let i = 0; i < length; i++) {\n\t\tresult += characters.charAt(Math.floor(Math.random() * charactersLength));\n\t}\n\n\treturn result;\n}\n\nclass InvalidRequestResponse extends Response {\n\tconstructor(message: string, status = 401) {\n\t\tsuper(JSON.stringify({ message }), {\n\t\t\tstatus,\n\t\t\theaders: {\n\t\t\t\t\"content-type\": \"application/json\",\n\t\t\t},\n\t\t});\n\t}\n}\n\nexport class ShopAuthorizeEvent<Shop extends ShopInterface = ShopInterface> {\n\tprivate reject: string | null = null;\n\n\tconstructor(\n\t\tpublic request: Request,\n\t\tpublic shop: Shop,\n\t) {}\n\n\tpublic rejectRegistration(reason: string) {\n\t\tthis.reject = reason;\n\t}\n\n\tpublic get reason() {\n\t\treturn this.reject;\n\t}\n}\n\nexport class AppInstallEvent<Shop extends ShopInterface = ShopInterface> {\n\tconstructor(\n\t\tpublic request: Request,\n\t\tpublic shop: Shop,\n\t\tpublic appVersion: string | null = null,\n\t) {}\n}\n\nexport class AppActivateEvent<Shop extends ShopInterface = ShopInterface> {\n\tconstructor(\n\t\tpublic request: Request,\n\t\tpublic shop: Shop,\n\t) {}\n}\n\nexport class AppDeactivateEvent<Shop extends ShopInterface = ShopInterface> {\n\tconstructor(\n\t\tpublic request: Request,\n\t\tpublic shop: Shop,\n\t) {}\n}\n\nexport class AppUpdateEvent<Shop extends ShopInterface = ShopInterface> {\n\tconstructor(\n\t\tpublic request: Request,\n\t\tpublic shop: Shop,\n\t\tpublic appVersion: string | null = null,\n\t) {}\n}\n\nexport class AppUninstallEvent<Shop extends ShopInterface = ShopInterface> {\n\tconstructor(\n\t\tpublic request: Request,\n\t\tpublic shop: Shop,\n\t\tpublic keepUserData: boolean | null = null,\n\t) {}\n}\n\nexport class BeforeRegistrationEvent<Shop extends ShopInterface = ShopInterface> {\n\tprivate cancellationReason: string | null = null;\n\t\n\tconstructor(\n\t\tpublic request: Request,\n\t\tpublic shopId: string,\n\t\tpublic shopUrl: string,\n\t) {}\n\n\tpublic rejectRegistration(reason: string) {\n\t\tthis.cancellationReason = reason;\n\t}\n\n\tpublic get reason() {\n\t\treturn this.cancellationReason;\n\t}\n}\n"]}
1
+ {"version":3,"file":"registration.js","sourceRoot":"","sources":["../../src/registration.ts"],"names":[],"mappings":"AAGA,MAAM,OAAO,YAAY;IACJ;IAApB,YAAoB,GAAoB;QAApB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,EAChB,MAAc,EACd,OAAe;QAFf,YAAO,GAAP,OAAO,CAAS;QAChB,WAAM,GAAN,MAAM,CAAQ;QACd,YAAO,GAAP,OAAO,CAAQ;IACpB,CAAC;IAEG,kBAAkB,CAAC,MAAc;QACvC,IAAI,CAAC,kBAAkB,GAAG,MAAM,CAAC;IAClC,CAAC;IAED,IAAW,MAAM;QAChB,OAAO,IAAI,CAAC,kBAAkB,CAAC;IAChC,CAAC;CACD","sourcesContent":["import type { AppServer } from \"./app.js\";\nimport type { ShopInterface } from \"./repository.js\";\n\nexport class Registration<Shop extends ShopInterface = ShopInterface> {\n\tconstructor(private app: AppServer<Shop>) {}\n\n\t/**\n\t * This method checks the request for the handshake with the Shopware Shop.\n\t * if it's valid a Shop will be created, and a proof will be responded with a confirmation url.\n\t * then the Shop will call the confirmation url, and this should be handled by the authorizeCallback method to finish the handshake.\n\t */\n\tpublic async authorize(req: Request): Promise<Response> {\n\t\tconst url = new URL(req.url);\n\n\t\tif (\n\t\t\t!url.searchParams.has(\"shop-url\") ||\n\t\t\t!req.headers.has(\"shopware-app-signature\") ||\n\t\t\t!url.searchParams.has(\"shop-id\") ||\n\t\t\t!url.searchParams.has(\"timestamp\")\n\t\t) {\n\t\t\treturn new InvalidRequestResponse(\"Invalid Request\", 400);\n\t\t}\n\n\t\tconst shopId = url.searchParams.get(\"shop-id\") as string;\n\t\tconst shopUrl = url.searchParams.get(\"shop-url\") as string;\n\t\tconst timestamp = url.searchParams.get(\"timestamp\") as string;\n\n\t\tconst beforeRegistrationEvent = new BeforeRegistrationEvent(req, shopId, shopUrl);\n\t\tthis.app.hooks.publish('onBeforeRegistrationEvent', beforeRegistrationEvent);\n\n\t\tif (beforeRegistrationEvent.reason) {\n\t\t\treturn new InvalidRequestResponse(beforeRegistrationEvent.reason, 400);\n\t\t}\n\n\t\tconst v = await this.app.signer.verify(\n\t\t\treq.headers.get(\"shopware-app-signature\") as string,\n\t\t\t`shop-id=${shopId}&shop-url=${shopUrl}&timestamp=${timestamp}`,\n\t\t\tthis.app.cfg.appSecret,\n\t\t);\n\n\t\tif (!v) {\n\t\t\treturn new InvalidRequestResponse(\"Cannot validate app signature\");\n\t\t}\n\n\t\tconst shopSecret = randomString();\n\t\tconst sanitizedShopUrl = shopUrl\n\t\t\t.replace(/([^:])(\\/\\/+)/g, \"$1/\")\n\t\t\t.replace(/\\/+$/, \"\");\n\n\t\tawait this.app.repository.createShop(shopId, sanitizedShopUrl, shopSecret);\n\n\t\treturn new Response(\n\t\t\tJSON.stringify({\n\t\t\t\tproof: await this.app.signer.sign(\n\t\t\t\t\tshopId + shopUrl + this.app.cfg.appName,\n\t\t\t\t\tthis.app.cfg.appSecret,\n\t\t\t\t),\n\t\t\t\tsecret: shopSecret,\n\t\t\t\tconfirmation_url: this.app.cfg.authorizeCallbackUrl,\n\t\t\t}),\n\t\t\t{\n\t\t\t\theaders: {\n\t\t\t\t\t\"content-type\": \"application/json\",\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\t}\n\n\t/**\n\t * This method is called by the Shopware Shop to confirm the handshake.\n\t * It will update the shop with the given oauth2 credentials.\n\t */\n\tpublic async authorizeCallback(req: Request): Promise<Response> {\n\t\tconst bodyContent = await req.text();\n\n\t\tconst body = JSON.parse(bodyContent);\n\n\t\tif (\n\t\t\ttypeof body.shopId !== \"string\" ||\n\t\t\ttypeof body.apiKey !== \"string\" ||\n\t\t\ttypeof body.secretKey !== \"string\" ||\n\t\t\t!req.headers.has(\"shopware-shop-signature\")\n\t\t) {\n\t\t\treturn new InvalidRequestResponse(\"Invalid Request\", 400);\n\t\t}\n\n\t\tconst shop = await this.app.repository.getShopById(body.shopId as string);\n\n\t\tif (shop === null) {\n\t\t\treturn new InvalidRequestResponse(\"Invalid shop given\");\n\t\t}\n\n\t\tconst v = await this.app.signer.verify(\n\t\t\treq.headers.get(\"shopware-shop-signature\") as string,\n\t\t\tbodyContent,\n\t\t\tshop.getShopSecret(),\n\t\t);\n\n\t\tif (!v) {\n\t\t\t// Shop has failed the verification. Delete it from our DB.\n\t\t\tawait this.app.repository.deleteShop(shop.getShopId());\n\n\t\t\treturn new InvalidRequestResponse(\"Cannot validate app signature\");\n\t\t}\n\n\t\tshop.setShopCredentials(body.apiKey, body.secretKey);\n\n\t\tconst event = new ShopAuthorizeEvent(req, shop);\n\t\tawait this.app.hooks.publish(\"onAuthorize\", event);\n\n\t\tif (event.reason) {\n\t\t\tawait this.app.repository.deleteShop(shop.getShopId());\n\n\t\t\treturn new InvalidRequestResponse(event.reason, 403);\n\t\t}\n\n\t\tawait this.app.repository.updateShop(shop);\n\n\t\treturn new Response(null, { status: 204 });\n\t}\n\n\t/**\n\t * This method should be called by Shopware Shop to set the shop active.\n\t *\n\t * <webhooks>\n\t * <webhook name=\"appActivate\" url=\"http://localhost:3000/app/activate\" event=\"app.activated\"/>\n\t * </webhooks>\n\t */\n\tpublic async activate(req: Request): Promise<Response> {\n\t\tconst ctx = await this.app.contextResolver.fromAPI(req);\n\n\t\tconst event = new AppActivateEvent(req, ctx.shop);\n\t\tawait this.app.hooks.publish(\"onAppActivate\", event);\n\n\t\tctx.shop.setShopActive(true);\n\n\t\tawait this.app.repository.updateShop(ctx.shop);\n\n\t\treturn new Response(null, { status: 204 });\n\t}\n\n\t/**\n\t * This method should be called by Shopware when the app was installed.\n\t *\n\t * <webhooks>\n\t * <webhook name=\"appInstall\" url=\"http://localhost:3000/app/install\" event=\"app.installed\"/>\n\t * </webhooks>\n\t */\n\tpublic async install(req: Request): Promise<Response> {\n\t\tconst ctx = await this.app.contextResolver.fromAPI<{\n\t\t\tdata: { payload: { appVersion: string } };\n\t\t}>(req);\n\n\t\tconst event = new AppInstallEvent(\n\t\t\treq,\n\t\t\tctx.shop,\n\t\t\tctx.payload?.data?.payload?.appVersion ?? null,\n\t\t);\n\t\tawait this.app.hooks.publish(\"onAppInstall\", event);\n\n\t\treturn new Response(null, { status: 204 });\n\t}\n\n\t/**\n\t * This method should be called by Shopware Shop to set the shop in-active.\n\t *\n\t * <webhooks>\n\t * <webhook name=\"appDeactivated\" url=\"http://localhost:3000/app/deactivated\" event=\"app.deactivated\"/>\n\t * </webhooks>\n\t */\n\tpublic async deactivate(req: Request): Promise<Response> {\n\t\tconst ctx = await this.app.contextResolver.fromAPI(req);\n\n\t\tconst event = new AppDeactivateEvent(req, ctx.shop);\n\t\tawait this.app.hooks.publish(\"onAppDeactivate\", event);\n\n\t\tctx.shop.setShopActive(false);\n\n\t\tawait this.app.repository.updateShop(ctx.shop);\n\n\t\treturn new Response(null, { status: 204 });\n\t}\n\n\t/**\n\t * This method should be called by Shopware when the app was updated.\n\t *\n\t * <webhooks>\n\t * <webhook name=\"appUpdated\" url=\"http://localhost:3000/app/update\" event=\"app.updated\"/>\n\t * </webhooks>\n\t */\n\tpublic async update(req: Request): Promise<Response> {\n\t\tconst ctx = await this.app.contextResolver.fromAPI<{\n\t\t\tdata: { payload: { appVersion: string } };\n\t\t}>(req);\n\n\t\tconst event = new AppUpdateEvent(\n\t\t\treq,\n\t\t\tctx.shop,\n\t\t\tctx.payload?.data?.payload?.appVersion ?? null,\n\t\t);\n\t\tawait this.app.hooks.publish(\"onAppUpdate\", event);\n\n\t\treturn new Response(null, { status: 204 });\n\t}\n\n\t/**\n\t * This method should be called by Shopware Shop to delete the app.\n\t *\n\t * <webhooks>\n\t * <webhook name=\"appDelete\" url=\"http://localhost:3000/app/delete\" event=\"app.deleted\"/>\n\t * </webhooks>\n\t */\n\tpublic async delete(req: Request): Promise<Response> {\n\t\tconst ctx = await this.app.contextResolver.fromAPI<{\n\t\t\tdata: { payload: { keepUserData?: boolean } };\n\t\t}>(req);\n\n\t\tconst event = new AppUninstallEvent(\n\t\t\treq,\n\t\t\tctx.shop,\n\t\t\tctx.payload?.data?.payload?.keepUserData ?? null,\n\t\t);\n\t\tawait this.app.hooks.publish(\"onAppUninstall\", event);\n\n\t\tif (event.keepUserData === false) {\n\t\t\tawait this.app.repository.deleteShop(ctx.shop.getShopId());\n\t\t}\n\n\t\treturn new Response(null, { status: 204 });\n\t}\n}\n\nexport function randomString(length = 120) {\n\tlet result = \"\";\n\tconst characters =\n\t\t\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\";\n\tconst charactersLength = characters.length;\n\tfor (let i = 0; i < length; i++) {\n\t\tresult += characters.charAt(Math.floor(Math.random() * charactersLength));\n\t}\n\n\treturn result;\n}\n\nclass InvalidRequestResponse extends Response {\n\tconstructor(error: string, status = 401) {\n\t\tsuper(JSON.stringify({ error }), {\n\t\t\tstatus,\n\t\t\theaders: {\n\t\t\t\t\"content-type\": \"application/json\",\n\t\t\t},\n\t\t});\n\t}\n}\n\nexport class ShopAuthorizeEvent<Shop extends ShopInterface = ShopInterface> {\n\tprivate reject: string | null = null;\n\n\tconstructor(\n\t\tpublic request: Request,\n\t\tpublic shop: Shop,\n\t) {}\n\n\tpublic rejectRegistration(reason: string) {\n\t\tthis.reject = reason;\n\t}\n\n\tpublic get reason() {\n\t\treturn this.reject;\n\t}\n}\n\nexport class AppInstallEvent<Shop extends ShopInterface = ShopInterface> {\n\tconstructor(\n\t\tpublic request: Request,\n\t\tpublic shop: Shop,\n\t\tpublic appVersion: string | null = null,\n\t) {}\n}\n\nexport class AppActivateEvent<Shop extends ShopInterface = ShopInterface> {\n\tconstructor(\n\t\tpublic request: Request,\n\t\tpublic shop: Shop,\n\t) {}\n}\n\nexport class AppDeactivateEvent<Shop extends ShopInterface = ShopInterface> {\n\tconstructor(\n\t\tpublic request: Request,\n\t\tpublic shop: Shop,\n\t) {}\n}\n\nexport class AppUpdateEvent<Shop extends ShopInterface = ShopInterface> {\n\tconstructor(\n\t\tpublic request: Request,\n\t\tpublic shop: Shop,\n\t\tpublic appVersion: string | null = null,\n\t) {}\n}\n\nexport class AppUninstallEvent<Shop extends ShopInterface = ShopInterface> {\n\tconstructor(\n\t\tpublic request: Request,\n\t\tpublic shop: Shop,\n\t\tpublic keepUserData: boolean | null = null,\n\t) {}\n}\n\nexport class BeforeRegistrationEvent<Shop extends ShopInterface = ShopInterface> {\n\tprivate cancellationReason: string | null = null;\n\t\n\tconstructor(\n\t\tpublic request: Request,\n\t\tpublic shopId: string,\n\t\tpublic shopUrl: string,\n\t) {}\n\n\tpublic rejectRegistration(reason: string) {\n\t\tthis.cancellationReason = reason;\n\t}\n\n\tpublic get reason() {\n\t\treturn this.cancellationReason;\n\t}\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shopware-ag/app-server-sdk",
3
- "version": "1.1.21",
3
+ "version": "1.1.23",
4
4
  "description": "App Server SDK for JavaScript",
5
5
  "type": "module",
6
6
  "license": "MIT",