vayu-ts 0.2.11 → 0.2.13

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
@@ -16,7 +16,10 @@ npm i vayu-ts
16
16
 
17
17
  ### Initialization
18
18
 
19
- Initialize the Vayu API client. The `baseUrl` parameter is optional and defaults to Vayu's public API servers.
19
+ Initialize the Vayu API client.
20
+ > ⚠️ **The `CLIENT_ID` environment variable is required to use the Vayu API client.**
21
+
22
+ The `baseUrl` parameter is optional and defaults to Vayu's public API servers.
20
23
 
21
24
  ```typescript
22
25
  import { Vayu } from 'vayu-ts';
@@ -193,6 +196,33 @@ await vayu.webhooks.subscribeToOverage('https://your-webhook-url.com/overage');
193
196
  // Subscribe to anonymous customer creation notifications
194
197
  await vayu.webhooks.subscribeToAnonymousCustomerCreated('https://your-webhook-url.com/your_path');
195
198
 
199
+ // Validate webhook security
200
+ app.post('/webhooks', (req, res) => {
201
+ const payload = JSON.stringify(req.body);
202
+ const timestamp = Number(req.headers['x-timestamp']);
203
+ const signature = String(req.headers['x-signature']);
204
+
205
+ // Important to login before verifying webhook signatures.
206
+ await vayu.login();
207
+
208
+ const isValid = vayu.verifyWebhookSignature({
209
+ payload,
210
+ timestamp,
211
+ signature,
212
+ });
213
+
214
+ if (!isValid) {
215
+ // Handle invalid token.
216
+ }
217
+
218
+ // Proceed with webhook handling
219
+ console.log('Webhook is valid', JSON.parse(payload));
220
+
221
+ // ... Handle event.
222
+
223
+ res.sendStatus(200);
224
+ });
225
+
196
226
  ```
197
227
 
198
228
  ## Features
@@ -7,6 +7,7 @@ export declare class ConfigurationService {
7
7
  static initialize(apiKey: string, baseServer: BaseServerConfiguration): ConfigurationService;
8
8
  static generateServerConfiguration(baseUrl?: string): BaseServerConfiguration;
9
9
  private accessToken;
10
+ private clientId;
10
11
  private expiresAt;
11
12
  constructor(apiKey: string, baseServer: BaseServerConfiguration);
12
13
  generateNewClient<T extends {
@@ -6,6 +6,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.ConfigurationService = void 0;
7
7
  const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
8
8
  const openapi_1 = require("../../openapi");
9
+ const utils_1 = require("../utils");
10
+ const CLIENT_ID_ENV_VAR_NAME = 'CLIENT_ID';
9
11
  const EXPIRATION_THRESHOLD = 1000 * 60 * 5;
10
12
  const BASE_URLS_MAP = new Map([
11
13
  ['https://connect.withvayu.com', openapi_1.server1],
@@ -37,6 +39,7 @@ class ConfigurationService {
37
39
  this.apiKey = apiKey;
38
40
  this.baseServer = baseServer;
39
41
  this.expiresAt = Date.now();
42
+ this.clientId = (0, utils_1.getRequiredEnvVar)(CLIENT_ID_ENV_VAR_NAME);
40
43
  }
41
44
  generateNewClient(ClientClass) {
42
45
  this.validateIsLoggedIn();
@@ -81,6 +84,7 @@ class ConfigurationService {
81
84
  await this.generateToken();
82
85
  }
83
86
  context.setHeaderParam('Authorization', `Bearer ${this.accessToken}`);
87
+ context.setHeaderParam('x-api-key', this.clientId);
84
88
  return context;
85
89
  },
86
90
  post: async (context) => (context),
@@ -0,0 +1,2 @@
1
+ export declare function getEnvVar<EnumType extends string = string>(name: string): EnumType;
2
+ export declare function getRequiredEnvVar<EnumType extends string = string>(name: string): EnumType;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getRequiredEnvVar = exports.getEnvVar = void 0;
4
+ function getEnvVar(name) {
5
+ return process.env[name];
6
+ }
7
+ exports.getEnvVar = getEnvVar;
8
+ function getRequiredEnvVar(name) {
9
+ const envVarValue = getEnvVar(name);
10
+ if (!envVarValue) {
11
+ throw new Error(`Environment variable ${name} is required`);
12
+ }
13
+ return envVarValue;
14
+ }
15
+ exports.getRequiredEnvVar = getRequiredEnvVar;
@@ -0,0 +1 @@
1
+ export * from './env-vars';
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./env-vars"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vayu-ts",
3
- "version": "0.2.11",
3
+ "version": "0.2.13",
4
4
  "description": "Simple and easy to use typescript package for utilizing vayu billing system",
5
5
  "main": "dist/index.js",
6
6
  "types": "./dist/index.d.ts",