@pinelab/vendure-plugin-sendcloud 1.0.2 → 1.1.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/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ # 1.1.0 (2023-10-24)
2
+
3
+ - Updated vendure to 2.1.1
@@ -14,6 +14,6 @@ export declare function transitionToShipped(orderService: OrderService, ctx: Req
14
14
  */
15
15
  export declare function transitionToDelivered(orderService: OrderService, ctx: RequestContext, order: Order, handler: ConfigurableOperationInput): Promise<Fulfillment>;
16
16
  /**
17
- * Throw an error if the transition failed
17
+ * Throws the error result if the transition failed
18
18
  */
19
19
  export declare function throwIfTransitionFailed(result: FulfillmentStateTransitionError | Fulfillment | AddFulfillmentToOrderResult): void;
@@ -43,20 +43,18 @@ async function transitionToDelivered(orderService, ctx, order, handler) {
43
43
  }
44
44
  exports.transitionToDelivered = transitionToDelivered;
45
45
  /**
46
- * Throw an error if the transition failed
46
+ * Throws the error result if the transition failed
47
47
  */
48
48
  function throwIfTransitionFailed(result) {
49
49
  const stateError = result;
50
- if (stateError.transitionError) {
51
- if (stateError.fromState === stateError.toState) {
52
- return; // If already 'Shipped', don't count this as an error
53
- }
54
- throw Error(`${stateError.message} - from ${stateError.fromState} to ${stateError.toState} - ${stateError.transitionError}`);
50
+ if (stateError.transitionError &&
51
+ stateError.fromState === stateError.toState) {
52
+ return; // If already 'Shipped', don't count this as an error
55
53
  }
56
54
  // It's not a stateTransition error
57
55
  const error = result;
58
56
  if (error.errorCode) {
59
- throw Error(`${error.errorCode}: ${error.message}`);
57
+ throw error;
60
58
  }
61
59
  }
62
60
  exports.throwIfTransitionFailed = throwIfTransitionFailed;
@@ -0,0 +1,10 @@
1
+ /// <reference types="node" />
2
+ import type { Request } from 'express';
3
+ export interface RequestWithRawBody extends Request {
4
+ rawBody: Buffer;
5
+ }
6
+ /**
7
+ * Middleware which adds the raw request body to the incoming message object. This is needed by
8
+ * Stripe to properly verify webhook events.
9
+ */
10
+ export declare const rawBodyMiddleware: import("connect").NextHandleFunction;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.rawBodyMiddleware = void 0;
4
+ const body_parser_1 = require("body-parser");
5
+ /**
6
+ * Middleware which adds the raw request body to the incoming message object. This is needed by
7
+ * Stripe to properly verify webhook events.
8
+ */
9
+ exports.rawBodyMiddleware = (0, body_parser_1.raw)({
10
+ type: '*/*',
11
+ verify(req, res, buf, encoding) {
12
+ if (Buffer.isBuffer(buf)) {
13
+ req.rawBody = Buffer.from(buf);
14
+ }
15
+ return true;
16
+ },
17
+ });
@@ -1,8 +1,7 @@
1
1
  import { Request } from 'express';
2
2
  import { SendcloudService } from './sendcloud.service';
3
- import { IncomingWebhookBody } from './types/sendcloud-api.types';
4
3
  export declare class SendcloudController {
5
4
  private sendcloudService;
6
5
  constructor(sendcloudService: SendcloudService);
7
- webhook(req: Request, body: IncomingWebhookBody, signature: string, channelToken: string): Promise<unknown>;
6
+ webhook(req: Request, signature: string, channelToken: string): Promise<unknown>;
8
7
  }
@@ -23,8 +23,9 @@ let SendcloudController = class SendcloudController {
23
23
  constructor(sendcloudService) {
24
24
  this.sendcloudService = sendcloudService;
25
25
  }
26
- async webhook(req, body, signature, channelToken) {
27
- const rawBody = req.rawBody || JSON.stringify(body); // TestEnvironment doesnt have middleware applied, so no rawBody available
26
+ async webhook(req, signature, channelToken) {
27
+ const body = JSON.parse(req.body.toString());
28
+ const rawBody = req.rawBody;
28
29
  const ctx = await this.sendcloudService.createContext(channelToken);
29
30
  const { client } = await this.sendcloudService.getClient(ctx);
30
31
  if (!client.isValidWebhook(rawBody, signature)) {
@@ -51,11 +52,10 @@ let SendcloudController = class SendcloudController {
51
52
  __decorate([
52
53
  (0, common_1.Post)('webhook/:channelToken'),
53
54
  __param(0, (0, common_1.Req)()),
54
- __param(1, (0, common_1.Body)()),
55
- __param(2, (0, common_1.Headers)(sendcloud_client_1.SendcloudClient.signatureHeader)),
56
- __param(3, (0, common_1.Param)('channelToken')),
55
+ __param(1, (0, common_1.Headers)(sendcloud_client_1.SendcloudClient.signatureHeader)),
56
+ __param(2, (0, common_1.Param)('channelToken')),
57
57
  __metadata("design:type", Function),
58
- __metadata("design:paramtypes", [Object, Object, String, String]),
58
+ __metadata("design:paramtypes", [Object, String, String]),
59
59
  __metadata("design:returntype", Promise)
60
60
  ], SendcloudController.prototype, "webhook", null);
61
61
  SendcloudController = __decorate([
@@ -12,7 +12,7 @@ var SendcloudPlugin_1;
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
13
  exports.SendcloudPlugin = void 0;
14
14
  const core_1 = require("@vendure/core");
15
- const apollo_server_core_1 = require("apollo-server-core");
15
+ const graphql_tag_1 = require("graphql-tag");
16
16
  const path_1 = __importDefault(require("path"));
17
17
  const sendcloud_resolver_1 = require("./api/sendcloud.resolver");
18
18
  const sendcloud_service_1 = require("./api/sendcloud.service");
@@ -20,7 +20,7 @@ const constants_1 = require("./api/constants");
20
20
  const sendcloud_controller_1 = require("./api/sendcloud.controller");
21
21
  const sendcloud_config_entity_1 = require("./api/sendcloud-config.entity");
22
22
  const sendcloud_handler_1 = require("./api/sendcloud.handler");
23
- const raw_body_1 = require("../../util/src/raw-body");
23
+ const raw_body_middleware_1 = require("../../util/src/raw-body.middleware");
24
24
  let SendcloudPlugin = SendcloudPlugin_1 = class SendcloudPlugin {
25
25
  static init(options) {
26
26
  this.options = options;
@@ -46,7 +46,7 @@ SendcloudPlugin.ui = {
46
46
  SendcloudPlugin = SendcloudPlugin_1 = __decorate([
47
47
  (0, core_1.VendurePlugin)({
48
48
  adminApiExtensions: {
49
- schema: (0, apollo_server_core_1.gql) `
49
+ schema: (0, graphql_tag_1.gql) `
50
50
  extend enum HistoryEntryType {
51
51
  SENDCLOUD_NOTIFICATION
52
52
  }
@@ -82,10 +82,13 @@ SendcloudPlugin = SendcloudPlugin_1 = __decorate([
82
82
  controllers: [sendcloud_controller_1.SendcloudController],
83
83
  entities: [sendcloud_config_entity_1.SendcloudConfigEntity],
84
84
  configuration: (config) => {
85
+ config.apiOptions.middleware.push({
86
+ route: '/sendcloud/webhook*',
87
+ handler: raw_body_middleware_1.rawBodyMiddleware,
88
+ beforeListen: true,
89
+ });
85
90
  config.shippingOptions.fulfillmentHandlers.push(sendcloud_handler_1.sendcloudHandler);
86
91
  config.authOptions.customPermissions.push(sendcloud_resolver_1.sendcloudPermission);
87
- // save rawBody for signature verification
88
- config.apiOptions.middleware.push((0, raw_body_1.createRawBodyMiddleWare)('/sendcloud/webhook*'));
89
92
  return config;
90
93
  },
91
94
  compatibility: '^2.0.0',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pinelab/vendure-plugin-sendcloud",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "description": "Vendure plugin for syncing orders with SendCloud",
5
5
  "author": "Martijn van de Brug <martijn@pinelab.studio>",
6
6
  "homepage": "https://pinelab-plugins.com/",
@@ -14,12 +14,13 @@
14
14
  "types": "dist/vendure-plugin-sendcloud/src/index.d.ts",
15
15
  "files": [
16
16
  "dist",
17
- "README.md"
17
+ "README.md",
18
+ "CHANGELOG.md"
18
19
  ],
19
20
  "scripts": {
20
21
  "build": "rimraf dist && tsc && copyfiles -u 1 'src/ui/**/*' dist/vendure-plugin-sendcloud/src/",
21
22
  "test": "vitest run",
22
23
  "start": "yarn ts-node test/dev-server.ts"
23
24
  },
24
- "gitHead": "8f332e7c6bd03bc5fc410ee5a614a6edad36f221"
25
+ "gitHead": "985a9e6ffd5cec67d1a3a4d924c9bd4bf7faa982"
25
26
  }
@@ -1,6 +0,0 @@
1
- import { Middleware } from '@vendure/core';
2
- /**
3
- * Set req.rawBody before any other middleware changes the body. Used to verify incoming request signatures for example.
4
- * @param route
5
- */
6
- export declare function createRawBodyMiddleWare(route: string): Middleware;
@@ -1,26 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.createRawBodyMiddleWare = void 0;
7
- const body_parser_1 = __importDefault(require("body-parser"));
8
- /**
9
- * Set req.rawBody before any other middleware changes the body. Used to verify incoming request signatures for example.
10
- * @param route
11
- */
12
- function createRawBodyMiddleWare(route) {
13
- return {
14
- route,
15
- beforeListen: true,
16
- handler: body_parser_1.default.json({
17
- verify(req, _, buf) {
18
- if (Buffer.isBuffer(buf)) {
19
- req.rawBody = Buffer.from(buf);
20
- }
21
- return true;
22
- },
23
- }),
24
- };
25
- }
26
- exports.createRawBodyMiddleWare = createRawBodyMiddleWare;