@astratra/payments 1.1.0 → 1.2.1

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/README.md CHANGED
@@ -139,6 +139,12 @@ app.use(csrf({ skip: isWebhook }));
139
139
  app.use(subscriptionGuard({ skip: isWebhook }));
140
140
  ```
141
141
 
142
+ Seules les requêtes `POST` sont exemptées (`methods` pour un prestataire qui appelle
143
+ autrement) : une requête d'une autre méthode dont le chemin finit comme le webhook
144
+ (`DELETE /api/finance/factures/webhook`, servie par une route `/factures/:id`) garde
145
+ toutes ses couches. Évite quand même, sous le préfixe, une route `POST` dont le
146
+ dernier segment est libre.
147
+
142
148
  Le préfixe **et** le suffixe ensemble : un préfixe seul exempterait toute une
143
149
  section de l'API — une zone de facturation entière sans CSRF ni
144
150
  authentification.
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@astratra/payments",
3
- "version": "1.1.0",
3
+ "version": "1.2.1",
4
4
  "description": "The payment webhook pipeline done once: raw-body signature checks, replay protection, and the answers that stop a provider retrying for days.",
5
5
  "engines": {
6
- "node": ">=20"
6
+ "node": ">=22"
7
7
  },
8
8
  "main": "src/index.js",
9
9
  "types": "src/index.d.ts",
@@ -11,9 +11,6 @@
11
11
  "files": [
12
12
  "src"
13
13
  ],
14
- "scripts": {
15
- "test": "jest"
16
- },
17
14
  "jest": {
18
15
  "testMatch": [
19
16
  "**/__tests__/**/*.test.js"
@@ -32,9 +29,12 @@
32
29
  "access": "public"
33
30
  },
34
31
  "devDependencies": {
35
- "express": "^4.18.3",
32
+ "express": "^4.18.3 || ^5.0.0",
36
33
  "jest": "30.4.2",
37
34
  "pg": "^8.13.1",
38
35
  "supertest": "^7.0.0"
36
+ },
37
+ "scripts": {
38
+ "test": "jest"
39
39
  }
40
- }
40
+ }
package/src/exemption.js CHANGED
@@ -26,6 +26,8 @@
26
26
  * @param {string} [options.prefix] e.g. '/api/payments/'
27
27
  * @param {string} [options.suffix] e.g. '/webhook'
28
28
  * @param {RegExp} [options.pattern] anything the above cannot express.
29
+ * @param {string[]} [options.methods] methods a webhook is called with. Default ['POST']: a request with another
30
+ * method is never exempt, whatever its path.
29
31
  * @returns {(req: object|string) => boolean}
30
32
  */
31
33
  function createWebhookExemption(options = {}) {
@@ -34,6 +36,12 @@ function createWebhookExemption(options = {}) {
34
36
  const suffix = options.suffix || null;
35
37
  const pattern = options.pattern || null;
36
38
 
39
+ /* Un prestataire appelle son webhook en POST. Une requête d'une autre méthode dont le chemin ressemble à celui du
40
+ webhook (« DELETE /api/finance/factures/webhook », servie par une route « /factures/:id ») n'est pas le webhook :
41
+ elle garde toutes ses couches. */
42
+ const methods = new Set((options.methods === undefined ? ['POST'] : options.methods).map((method) => String(method).toUpperCase()));
43
+ if (!methods.size) throw new Error('createWebhookExemption: options.methods must name at least one method.');
44
+
37
45
  if (!paths.size && !prefix && !suffix && !pattern) {
38
46
  throw new Error('createWebhookExemption requires at least one of paths, prefix, suffix or pattern.');
39
47
  }
@@ -47,6 +55,9 @@ function createWebhookExemption(options = {}) {
47
55
  };
48
56
 
49
57
  return function isWebhook(input) {
58
+ /* Un chemin donné en texte, ou un objet sans méthode, se compare par le chemin seul. */
59
+ const method = input && typeof input === 'object' && typeof input.method === 'string' ? input.method.toUpperCase() : null;
60
+ if (method && !methods.has(method)) return false;
50
61
  const path = pathOf(input);
51
62
  if (!path) return false;
52
63
  if (paths.has(path)) return true;
package/src/index.d.ts CHANGED
@@ -132,6 +132,8 @@ export function createWebhookExemption(options: {
132
132
  prefix?: string;
133
133
  suffix?: string;
134
134
  pattern?: RegExp;
135
+ /** Methods a webhook is called with. Default ['POST']: a request with another method is never exempt. */
136
+ methods?: string[];
135
137
  }): (input: RequestLike | string | null) => boolean;
136
138
 
137
139
  /* ─────────────────── The client side of paying ─────────────────── */
package/src/webhook.js CHANGED
@@ -114,7 +114,9 @@ function createWebhookHandler(options = {}) {
114
114
 
115
115
  const id = eventIdOf(event);
116
116
 
117
- const handler = events[event.type];
117
+ /* Un traitement est une fonction déclarée dans `events`, jamais une propriété héritée de tout objet
118
+ (« constructor », « toString »…). */
119
+ const handler = typeof event.type === 'string' && Object.hasOwn(events, event.type) && typeof events[event.type] === 'function' ? events[event.type] : null;
118
120
  if (!handler) {
119
121
  /* Not an error. One endpoint receives everything the account emits, and
120
122
  most of it belongs to flows this code knows nothing about. */