@edirect/tokenization 0.0.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.
@@ -0,0 +1,105 @@
1
+ /**
2
+ * @file This file contains the models used in the application.
3
+ */
4
+
5
+ /**
6
+ * The classification of a field.
7
+ * 0: The field is not sensitive.
8
+ * 1: The field is sensitive.
9
+ */
10
+ type Classification = 0 | 1;
11
+ /**
12
+ * The model of a field classification.
13
+ * @property path The path of the field.
14
+ * @property classification The classification of the field.
15
+ * @example
16
+ * {
17
+ * path: "data.*.name",
18
+ * classification: 1
19
+ * }
20
+ * @see Classification
21
+ */
22
+ type FieldClassification = {
23
+ path: string;
24
+ classification: Classification;
25
+ };
26
+ /**
27
+ * The model of a configuration.
28
+ * @property key The key of the configuration.
29
+ * @property tenant The tenant of the configuration.
30
+ * @property defaultClassification The default classification of the configuration.
31
+ * @property fields The fields of the configuration.
32
+ * @example
33
+ * {
34
+ * key: "configuration",
35
+ * tenant: "tenant",
36
+ * defaultClassification: 0,
37
+ * fields: [
38
+ * {
39
+ * path: "data.*.name",
40
+ * classification: 1
41
+ * }
42
+ * ]
43
+ * }
44
+ * @see FieldClassification
45
+ * @see Classification
46
+ */
47
+ type Configuration = {
48
+ key: string;
49
+ tenant: string;
50
+ defaultClassification: Classification;
51
+ fields: FieldClassification[];
52
+ };
53
+ /**
54
+ * The model of a token payload.
55
+ */
56
+ type TokenPayload = Record<string, unknown>;
57
+ type TokenizationClientConfig = {
58
+ services?: {
59
+ tokenizationService?: ITokenizationService;
60
+ configurationService?: IConfigurationService;
61
+ };
62
+ };
63
+
64
+ interface ITokenizationService {
65
+ tokenize(auth: string, tenant: string, config: string, payload: TokenPayload): Promise<TokenPayload>;
66
+ detokenize(auth: string, tenant: string, config: string, payload: TokenPayload): Promise<TokenPayload>;
67
+ }
68
+ interface IConfigurationService {
69
+ get(auth: string, tenant: string, config: string): Promise<Configuration | undefined>;
70
+ create(auth: string, tenant: string, config: Configuration): Promise<Configuration>;
71
+ update(auth: string, tenant: string, config: Configuration): Promise<Configuration>;
72
+ delete(auth: string, tenant: string, config: string): Promise<void>;
73
+ }
74
+ interface ITokenizationApp {
75
+ tokenize(auth: string, tenant: string, config: string, payload: TokenPayload): Promise<TokenPayload>;
76
+ detokenize(auth: string, tenant: string, config: string, payload: TokenPayload): Promise<TokenPayload>;
77
+ }
78
+
79
+ /**
80
+ * The TokenizationClient class.
81
+ */
82
+ declare class Tokenization {
83
+ private app;
84
+ /**
85
+ * The constructor of the TokenizationClient class.
86
+ * @param url The URL of the tokenization server.
87
+ */
88
+ constructor(configOrApp?: TokenizationClientConfig | ITokenizationApp);
89
+ /**
90
+ * Tokenizes the given payload.
91
+ * @param tenant The tenant to tokenize the payload for.
92
+ * @param payload The payload to tokenize.
93
+ * @returns The tokenized payload.
94
+ */
95
+ tokenize(auth: string, tenant: string, config: string, payload: TokenPayload): Promise<TokenPayload>;
96
+ /**
97
+ * Detokenizes the given payload.
98
+ * @param tenant The tenant to detokenize the payload for.
99
+ * @param payload The payload to detokenize.
100
+ * @returns The detokenized payload.
101
+ */
102
+ detokenize(auth: string, tenant: string, config: string, payload: TokenPayload): Promise<TokenPayload>;
103
+ }
104
+
105
+ export { Tokenization };
@@ -0,0 +1,105 @@
1
+ /**
2
+ * @file This file contains the models used in the application.
3
+ */
4
+
5
+ /**
6
+ * The classification of a field.
7
+ * 0: The field is not sensitive.
8
+ * 1: The field is sensitive.
9
+ */
10
+ type Classification = 0 | 1;
11
+ /**
12
+ * The model of a field classification.
13
+ * @property path The path of the field.
14
+ * @property classification The classification of the field.
15
+ * @example
16
+ * {
17
+ * path: "data.*.name",
18
+ * classification: 1
19
+ * }
20
+ * @see Classification
21
+ */
22
+ type FieldClassification = {
23
+ path: string;
24
+ classification: Classification;
25
+ };
26
+ /**
27
+ * The model of a configuration.
28
+ * @property key The key of the configuration.
29
+ * @property tenant The tenant of the configuration.
30
+ * @property defaultClassification The default classification of the configuration.
31
+ * @property fields The fields of the configuration.
32
+ * @example
33
+ * {
34
+ * key: "configuration",
35
+ * tenant: "tenant",
36
+ * defaultClassification: 0,
37
+ * fields: [
38
+ * {
39
+ * path: "data.*.name",
40
+ * classification: 1
41
+ * }
42
+ * ]
43
+ * }
44
+ * @see FieldClassification
45
+ * @see Classification
46
+ */
47
+ type Configuration = {
48
+ key: string;
49
+ tenant: string;
50
+ defaultClassification: Classification;
51
+ fields: FieldClassification[];
52
+ };
53
+ /**
54
+ * The model of a token payload.
55
+ */
56
+ type TokenPayload = Record<string, unknown>;
57
+ type TokenizationClientConfig = {
58
+ services?: {
59
+ tokenizationService?: ITokenizationService;
60
+ configurationService?: IConfigurationService;
61
+ };
62
+ };
63
+
64
+ interface ITokenizationService {
65
+ tokenize(auth: string, tenant: string, config: string, payload: TokenPayload): Promise<TokenPayload>;
66
+ detokenize(auth: string, tenant: string, config: string, payload: TokenPayload): Promise<TokenPayload>;
67
+ }
68
+ interface IConfigurationService {
69
+ get(auth: string, tenant: string, config: string): Promise<Configuration | undefined>;
70
+ create(auth: string, tenant: string, config: Configuration): Promise<Configuration>;
71
+ update(auth: string, tenant: string, config: Configuration): Promise<Configuration>;
72
+ delete(auth: string, tenant: string, config: string): Promise<void>;
73
+ }
74
+ interface ITokenizationApp {
75
+ tokenize(auth: string, tenant: string, config: string, payload: TokenPayload): Promise<TokenPayload>;
76
+ detokenize(auth: string, tenant: string, config: string, payload: TokenPayload): Promise<TokenPayload>;
77
+ }
78
+
79
+ /**
80
+ * The TokenizationClient class.
81
+ */
82
+ declare class Tokenization {
83
+ private app;
84
+ /**
85
+ * The constructor of the TokenizationClient class.
86
+ * @param url The URL of the tokenization server.
87
+ */
88
+ constructor(configOrApp?: TokenizationClientConfig | ITokenizationApp);
89
+ /**
90
+ * Tokenizes the given payload.
91
+ * @param tenant The tenant to tokenize the payload for.
92
+ * @param payload The payload to tokenize.
93
+ * @returns The tokenized payload.
94
+ */
95
+ tokenize(auth: string, tenant: string, config: string, payload: TokenPayload): Promise<TokenPayload>;
96
+ /**
97
+ * Detokenizes the given payload.
98
+ * @param tenant The tenant to detokenize the payload for.
99
+ * @param payload The payload to detokenize.
100
+ * @returns The detokenized payload.
101
+ */
102
+ detokenize(auth: string, tenant: string, config: string, payload: TokenPayload): Promise<TokenPayload>;
103
+ }
104
+
105
+ export { Tokenization };
package/dist/index.js ADDED
@@ -0,0 +1,336 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ Tokenization: () => Tokenization
24
+ });
25
+ module.exports = __toCommonJS(src_exports);
26
+
27
+ // src/core/traverser/index.ts
28
+ var traverse = (obj, callback, path = []) => {
29
+ if (Array.isArray(obj)) {
30
+ obj.forEach((value, index) => {
31
+ traverse(value, callback, [...path, `${index}`]);
32
+ });
33
+ } else if (obj !== null && typeof obj === "object") {
34
+ Object.keys(obj).forEach((key) => {
35
+ traverse(obj[key], callback, [...path, key]);
36
+ });
37
+ } else {
38
+ callback(obj, path);
39
+ }
40
+ };
41
+
42
+ // src/core/utils/object.ts
43
+ var set = (obj, path, value) => {
44
+ let currentObj = obj;
45
+ for (let i = 0; i < path.length; i++) {
46
+ const key = path[i];
47
+ if (i === path.length - 1) {
48
+ currentObj[key] = value;
49
+ } else {
50
+ if (!currentObj[key] || typeof currentObj[key] !== "object") {
51
+ currentObj[key] = isNaN(Number(path[i + 1])) ? {} : [];
52
+ }
53
+ currentObj = currentObj[key];
54
+ }
55
+ }
56
+ };
57
+
58
+ // src/core/evaluator/basic.ts
59
+ var BasicEvaluator = class {
60
+ Evaluate(path, ...properties) {
61
+ const pathSegments = path.split(".");
62
+ const propertiesSegments = properties;
63
+ let i = 0, j = 0;
64
+ while (i < pathSegments.length && j < propertiesSegments.length) {
65
+ if (pathSegments[i] === "**") {
66
+ if (i === pathSegments.length - 1) {
67
+ return true;
68
+ }
69
+ i++;
70
+ while (j < propertiesSegments.length && propertiesSegments[j] !== pathSegments[i]) {
71
+ j++;
72
+ }
73
+ } else if (pathSegments[i] === "*" || pathSegments[i] === propertiesSegments[j]) {
74
+ i++;
75
+ j++;
76
+ } else {
77
+ return false;
78
+ }
79
+ }
80
+ return i === pathSegments.length && j === propertiesSegments.length;
81
+ }
82
+ };
83
+
84
+ // src/core/evaluator/index.ts
85
+ var TokenEvaluator = class {
86
+ evaluator;
87
+ constructor(evaluator) {
88
+ this.evaluator = evaluator || new BasicEvaluator();
89
+ }
90
+ shouldTokenizeField(config, path) {
91
+ if (!config) {
92
+ return false;
93
+ }
94
+ if (config.fields.length === 0) {
95
+ return config.defaultClassification > 0;
96
+ }
97
+ const ret = config.fields.map((field) => {
98
+ return this.evaluator.Evaluate(field.path, ...path);
99
+ });
100
+ return ret.some((r) => r) || config.defaultClassification > 0;
101
+ }
102
+ };
103
+
104
+ // src/core/app.ts
105
+ var TokenizationApp = class {
106
+ tokenizationService;
107
+ configurationService;
108
+ evaluator;
109
+ constructor(tokenizationService, configurationService, evaluator) {
110
+ this.tokenizationService = tokenizationService;
111
+ this.configurationService = configurationService;
112
+ this.evaluator = new TokenEvaluator(evaluator);
113
+ }
114
+ async tokenize(auth, tenant, configKey, payload) {
115
+ const config = await this.configurationService.get(auth, tenant, configKey);
116
+ if (!config) return payload;
117
+ if ((!config.fields || config.fields.length === 0) && config.defaultClassification !== 0)
118
+ return payload;
119
+ const processedPayload = await this.processRequest(config, payload);
120
+ const tokenPayload = await this.tokenizationService.tokenize(
121
+ auth,
122
+ tenant,
123
+ configKey,
124
+ processedPayload
125
+ );
126
+ const respPayload = payload;
127
+ for (const key in tokenPayload) {
128
+ set(respPayload, key.split("."), tokenPayload[key]);
129
+ }
130
+ return respPayload;
131
+ }
132
+ async detokenize(auth, tenant, configKey, payload) {
133
+ const config = await this.configurationService.get(auth, tenant, configKey);
134
+ if (!config) return payload;
135
+ if ((!config.fields || config.fields.length === 0) && config.defaultClassification !== 0)
136
+ return payload;
137
+ const processedPayload = this.processRequest(config, payload);
138
+ const tokenPayload = await this.tokenizationService.detokenize(
139
+ auth,
140
+ tenant,
141
+ configKey,
142
+ processedPayload
143
+ );
144
+ const respPayload = payload;
145
+ for (const key in tokenPayload) {
146
+ set(respPayload, key.split("."), tokenPayload[key]);
147
+ }
148
+ return respPayload;
149
+ }
150
+ processRequest(config, payload) {
151
+ if (config.fields.length === 0) {
152
+ return payload;
153
+ }
154
+ const req = {};
155
+ traverse(payload, (value, path) => {
156
+ if (this.evaluator.shouldTokenizeField(config, path)) {
157
+ req[path.join(".")] = value;
158
+ }
159
+ });
160
+ return req;
161
+ }
162
+ };
163
+
164
+ // src/core/services/configuration.ts
165
+ var ConfigurationService = class {
166
+ constructor(baseUrl) {
167
+ this.baseUrl = baseUrl;
168
+ if (!this.baseUrl) {
169
+ throw new Error("Configuration Service BaseUrl is required");
170
+ }
171
+ }
172
+ async get(auth, tenant, config) {
173
+ const resp = await fetch(
174
+ `${this.baseUrl}/api/v1/${tenant}/${config}/config`,
175
+ {
176
+ method: "GET",
177
+ headers: {
178
+ Authorization: `Bearer ${auth}`
179
+ }
180
+ }
181
+ );
182
+ if (resp.status === 404) {
183
+ return void 0;
184
+ }
185
+ if (resp.status !== 200) {
186
+ throw new Error("Failed to get configuration");
187
+ }
188
+ const data = await resp.json();
189
+ return data;
190
+ }
191
+ async create(auth, tenant, config) {
192
+ const resp = await fetch(
193
+ `${this.baseUrl}/api/v1/${tenant}/${config.key}/config`,
194
+ {
195
+ method: "POST",
196
+ body: JSON.stringify(config),
197
+ headers: {
198
+ Authorization: `Bearer ${auth}`,
199
+ "Content-Type": "application/json"
200
+ }
201
+ }
202
+ );
203
+ if (resp.status !== 200) {
204
+ throw new Error("Failed to create configuration");
205
+ }
206
+ return config;
207
+ }
208
+ async update(auth, tenant, config) {
209
+ const respo = await fetch(
210
+ `${this.baseUrl}/api/v1/${tenant}/${config.key}/config`,
211
+ {
212
+ method: "PUT",
213
+ body: JSON.stringify(config),
214
+ headers: {
215
+ Authorization: `Bearer ${auth}`,
216
+ "Content-Type": "application/json"
217
+ }
218
+ }
219
+ );
220
+ if (respo.status !== 200) {
221
+ throw new Error("Failed to update configuration");
222
+ }
223
+ return config;
224
+ }
225
+ async delete(auth, tenant, config) {
226
+ const resp = await fetch(
227
+ `${this.baseUrl}/api/v1/${tenant}/${config}/config`,
228
+ {
229
+ method: "DELETE",
230
+ headers: {
231
+ Authorization: `Bearer ${auth}`
232
+ }
233
+ }
234
+ );
235
+ if (resp.status !== 204) {
236
+ throw new Error("Failed to delete configuration");
237
+ }
238
+ }
239
+ };
240
+
241
+ // src/core/services/tokenization.ts
242
+ var TokenizationService = class {
243
+ constructor(baseUrl) {
244
+ this.baseUrl = baseUrl;
245
+ if (!this.baseUrl) {
246
+ throw new Error("Tokenization Service BaseUrl is required");
247
+ }
248
+ }
249
+ async tokenize(auth, tenant, config, payload) {
250
+ const resp = await fetch(
251
+ `${this.baseUrl}/api/v1/${tenant}/${config}/token/tokenize`,
252
+ {
253
+ method: "POST",
254
+ body: JSON.stringify(payload),
255
+ headers: {
256
+ "Authorization": `Bearer ${auth}`,
257
+ "Content-Type": "application/json"
258
+ }
259
+ }
260
+ );
261
+ if (resp.status !== 200) {
262
+ throw new Error("Failed to tokenize payload");
263
+ }
264
+ const data = await resp.json();
265
+ return data;
266
+ }
267
+ async detokenize(auth, tenant, config, payload) {
268
+ const resp = await fetch(
269
+ `${this.baseUrl}/api/v1/${tenant}/${config}/token/detokenize`,
270
+ {
271
+ method: "POST",
272
+ body: JSON.stringify(payload),
273
+ headers: {
274
+ "Authorization": `Bearer ${auth}`,
275
+ "Content-Type": "application/json"
276
+ }
277
+ }
278
+ );
279
+ if (resp.status !== 200) {
280
+ throw new Error("Failed to detokenize payload");
281
+ }
282
+ const data = await resp.json();
283
+ return data;
284
+ }
285
+ };
286
+
287
+ // src/index.ts
288
+ var Tokenization = class {
289
+ app;
290
+ /**
291
+ * The constructor of the TokenizationClient class.
292
+ * @param url The URL of the tokenization server.
293
+ */
294
+ constructor(configOrApp) {
295
+ if (configOrApp && "tokenize" in configOrApp && "detokenize" in configOrApp) {
296
+ this.app = configOrApp;
297
+ return;
298
+ }
299
+ if (configOrApp && "services" in configOrApp && configOrApp.services && "tokenizationService" in configOrApp.services && "configurationService" in configOrApp.services && configOrApp.services.tokenizationService && configOrApp.services.configurationService) {
300
+ const tokenizationService2 = configOrApp.services.tokenizationService;
301
+ const configurationService2 = configOrApp.services.configurationService;
302
+ this.app = new TokenizationApp(tokenizationService2, configurationService2);
303
+ return;
304
+ }
305
+ const tokenizationService = new TokenizationService(
306
+ process.env.TOKENIZATION_SERVICE_BASE_URL
307
+ );
308
+ const configurationService = new ConfigurationService(
309
+ process.env.CONFIGURATION_SERVICE_BASE_URL
310
+ );
311
+ this.app = new TokenizationApp(tokenizationService, configurationService);
312
+ return;
313
+ }
314
+ /**
315
+ * Tokenizes the given payload.
316
+ * @param tenant The tenant to tokenize the payload for.
317
+ * @param payload The payload to tokenize.
318
+ * @returns The tokenized payload.
319
+ */
320
+ tokenize(auth, tenant, config, payload) {
321
+ return this.app.tokenize(auth, tenant, config, payload);
322
+ }
323
+ /**
324
+ * Detokenizes the given payload.
325
+ * @param tenant The tenant to detokenize the payload for.
326
+ * @param payload The payload to detokenize.
327
+ * @returns The detokenized payload.
328
+ */
329
+ detokenize(auth, tenant, config, payload) {
330
+ return this.app.detokenize(auth, tenant, config, payload);
331
+ }
332
+ };
333
+ // Annotate the CommonJS export names for ESM import in node:
334
+ 0 && (module.exports = {
335
+ Tokenization
336
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,309 @@
1
+ // src/core/traverser/index.ts
2
+ var traverse = (obj, callback, path = []) => {
3
+ if (Array.isArray(obj)) {
4
+ obj.forEach((value, index) => {
5
+ traverse(value, callback, [...path, `${index}`]);
6
+ });
7
+ } else if (obj !== null && typeof obj === "object") {
8
+ Object.keys(obj).forEach((key) => {
9
+ traverse(obj[key], callback, [...path, key]);
10
+ });
11
+ } else {
12
+ callback(obj, path);
13
+ }
14
+ };
15
+
16
+ // src/core/utils/object.ts
17
+ var set = (obj, path, value) => {
18
+ let currentObj = obj;
19
+ for (let i = 0; i < path.length; i++) {
20
+ const key = path[i];
21
+ if (i === path.length - 1) {
22
+ currentObj[key] = value;
23
+ } else {
24
+ if (!currentObj[key] || typeof currentObj[key] !== "object") {
25
+ currentObj[key] = isNaN(Number(path[i + 1])) ? {} : [];
26
+ }
27
+ currentObj = currentObj[key];
28
+ }
29
+ }
30
+ };
31
+
32
+ // src/core/evaluator/basic.ts
33
+ var BasicEvaluator = class {
34
+ Evaluate(path, ...properties) {
35
+ const pathSegments = path.split(".");
36
+ const propertiesSegments = properties;
37
+ let i = 0, j = 0;
38
+ while (i < pathSegments.length && j < propertiesSegments.length) {
39
+ if (pathSegments[i] === "**") {
40
+ if (i === pathSegments.length - 1) {
41
+ return true;
42
+ }
43
+ i++;
44
+ while (j < propertiesSegments.length && propertiesSegments[j] !== pathSegments[i]) {
45
+ j++;
46
+ }
47
+ } else if (pathSegments[i] === "*" || pathSegments[i] === propertiesSegments[j]) {
48
+ i++;
49
+ j++;
50
+ } else {
51
+ return false;
52
+ }
53
+ }
54
+ return i === pathSegments.length && j === propertiesSegments.length;
55
+ }
56
+ };
57
+
58
+ // src/core/evaluator/index.ts
59
+ var TokenEvaluator = class {
60
+ evaluator;
61
+ constructor(evaluator) {
62
+ this.evaluator = evaluator || new BasicEvaluator();
63
+ }
64
+ shouldTokenizeField(config, path) {
65
+ if (!config) {
66
+ return false;
67
+ }
68
+ if (config.fields.length === 0) {
69
+ return config.defaultClassification > 0;
70
+ }
71
+ const ret = config.fields.map((field) => {
72
+ return this.evaluator.Evaluate(field.path, ...path);
73
+ });
74
+ return ret.some((r) => r) || config.defaultClassification > 0;
75
+ }
76
+ };
77
+
78
+ // src/core/app.ts
79
+ var TokenizationApp = class {
80
+ tokenizationService;
81
+ configurationService;
82
+ evaluator;
83
+ constructor(tokenizationService, configurationService, evaluator) {
84
+ this.tokenizationService = tokenizationService;
85
+ this.configurationService = configurationService;
86
+ this.evaluator = new TokenEvaluator(evaluator);
87
+ }
88
+ async tokenize(auth, tenant, configKey, payload) {
89
+ const config = await this.configurationService.get(auth, tenant, configKey);
90
+ if (!config) return payload;
91
+ if ((!config.fields || config.fields.length === 0) && config.defaultClassification !== 0)
92
+ return payload;
93
+ const processedPayload = await this.processRequest(config, payload);
94
+ const tokenPayload = await this.tokenizationService.tokenize(
95
+ auth,
96
+ tenant,
97
+ configKey,
98
+ processedPayload
99
+ );
100
+ const respPayload = payload;
101
+ for (const key in tokenPayload) {
102
+ set(respPayload, key.split("."), tokenPayload[key]);
103
+ }
104
+ return respPayload;
105
+ }
106
+ async detokenize(auth, tenant, configKey, payload) {
107
+ const config = await this.configurationService.get(auth, tenant, configKey);
108
+ if (!config) return payload;
109
+ if ((!config.fields || config.fields.length === 0) && config.defaultClassification !== 0)
110
+ return payload;
111
+ const processedPayload = this.processRequest(config, payload);
112
+ const tokenPayload = await this.tokenizationService.detokenize(
113
+ auth,
114
+ tenant,
115
+ configKey,
116
+ processedPayload
117
+ );
118
+ const respPayload = payload;
119
+ for (const key in tokenPayload) {
120
+ set(respPayload, key.split("."), tokenPayload[key]);
121
+ }
122
+ return respPayload;
123
+ }
124
+ processRequest(config, payload) {
125
+ if (config.fields.length === 0) {
126
+ return payload;
127
+ }
128
+ const req = {};
129
+ traverse(payload, (value, path) => {
130
+ if (this.evaluator.shouldTokenizeField(config, path)) {
131
+ req[path.join(".")] = value;
132
+ }
133
+ });
134
+ return req;
135
+ }
136
+ };
137
+
138
+ // src/core/services/configuration.ts
139
+ var ConfigurationService = class {
140
+ constructor(baseUrl) {
141
+ this.baseUrl = baseUrl;
142
+ if (!this.baseUrl) {
143
+ throw new Error("Configuration Service BaseUrl is required");
144
+ }
145
+ }
146
+ async get(auth, tenant, config) {
147
+ const resp = await fetch(
148
+ `${this.baseUrl}/api/v1/${tenant}/${config}/config`,
149
+ {
150
+ method: "GET",
151
+ headers: {
152
+ Authorization: `Bearer ${auth}`
153
+ }
154
+ }
155
+ );
156
+ if (resp.status === 404) {
157
+ return void 0;
158
+ }
159
+ if (resp.status !== 200) {
160
+ throw new Error("Failed to get configuration");
161
+ }
162
+ const data = await resp.json();
163
+ return data;
164
+ }
165
+ async create(auth, tenant, config) {
166
+ const resp = await fetch(
167
+ `${this.baseUrl}/api/v1/${tenant}/${config.key}/config`,
168
+ {
169
+ method: "POST",
170
+ body: JSON.stringify(config),
171
+ headers: {
172
+ Authorization: `Bearer ${auth}`,
173
+ "Content-Type": "application/json"
174
+ }
175
+ }
176
+ );
177
+ if (resp.status !== 200) {
178
+ throw new Error("Failed to create configuration");
179
+ }
180
+ return config;
181
+ }
182
+ async update(auth, tenant, config) {
183
+ const respo = await fetch(
184
+ `${this.baseUrl}/api/v1/${tenant}/${config.key}/config`,
185
+ {
186
+ method: "PUT",
187
+ body: JSON.stringify(config),
188
+ headers: {
189
+ Authorization: `Bearer ${auth}`,
190
+ "Content-Type": "application/json"
191
+ }
192
+ }
193
+ );
194
+ if (respo.status !== 200) {
195
+ throw new Error("Failed to update configuration");
196
+ }
197
+ return config;
198
+ }
199
+ async delete(auth, tenant, config) {
200
+ const resp = await fetch(
201
+ `${this.baseUrl}/api/v1/${tenant}/${config}/config`,
202
+ {
203
+ method: "DELETE",
204
+ headers: {
205
+ Authorization: `Bearer ${auth}`
206
+ }
207
+ }
208
+ );
209
+ if (resp.status !== 204) {
210
+ throw new Error("Failed to delete configuration");
211
+ }
212
+ }
213
+ };
214
+
215
+ // src/core/services/tokenization.ts
216
+ var TokenizationService = class {
217
+ constructor(baseUrl) {
218
+ this.baseUrl = baseUrl;
219
+ if (!this.baseUrl) {
220
+ throw new Error("Tokenization Service BaseUrl is required");
221
+ }
222
+ }
223
+ async tokenize(auth, tenant, config, payload) {
224
+ const resp = await fetch(
225
+ `${this.baseUrl}/api/v1/${tenant}/${config}/token/tokenize`,
226
+ {
227
+ method: "POST",
228
+ body: JSON.stringify(payload),
229
+ headers: {
230
+ "Authorization": `Bearer ${auth}`,
231
+ "Content-Type": "application/json"
232
+ }
233
+ }
234
+ );
235
+ if (resp.status !== 200) {
236
+ throw new Error("Failed to tokenize payload");
237
+ }
238
+ const data = await resp.json();
239
+ return data;
240
+ }
241
+ async detokenize(auth, tenant, config, payload) {
242
+ const resp = await fetch(
243
+ `${this.baseUrl}/api/v1/${tenant}/${config}/token/detokenize`,
244
+ {
245
+ method: "POST",
246
+ body: JSON.stringify(payload),
247
+ headers: {
248
+ "Authorization": `Bearer ${auth}`,
249
+ "Content-Type": "application/json"
250
+ }
251
+ }
252
+ );
253
+ if (resp.status !== 200) {
254
+ throw new Error("Failed to detokenize payload");
255
+ }
256
+ const data = await resp.json();
257
+ return data;
258
+ }
259
+ };
260
+
261
+ // src/index.ts
262
+ var Tokenization = class {
263
+ app;
264
+ /**
265
+ * The constructor of the TokenizationClient class.
266
+ * @param url The URL of the tokenization server.
267
+ */
268
+ constructor(configOrApp) {
269
+ if (configOrApp && "tokenize" in configOrApp && "detokenize" in configOrApp) {
270
+ this.app = configOrApp;
271
+ return;
272
+ }
273
+ if (configOrApp && "services" in configOrApp && configOrApp.services && "tokenizationService" in configOrApp.services && "configurationService" in configOrApp.services && configOrApp.services.tokenizationService && configOrApp.services.configurationService) {
274
+ const tokenizationService2 = configOrApp.services.tokenizationService;
275
+ const configurationService2 = configOrApp.services.configurationService;
276
+ this.app = new TokenizationApp(tokenizationService2, configurationService2);
277
+ return;
278
+ }
279
+ const tokenizationService = new TokenizationService(
280
+ process.env.TOKENIZATION_SERVICE_BASE_URL
281
+ );
282
+ const configurationService = new ConfigurationService(
283
+ process.env.CONFIGURATION_SERVICE_BASE_URL
284
+ );
285
+ this.app = new TokenizationApp(tokenizationService, configurationService);
286
+ return;
287
+ }
288
+ /**
289
+ * Tokenizes the given payload.
290
+ * @param tenant The tenant to tokenize the payload for.
291
+ * @param payload The payload to tokenize.
292
+ * @returns The tokenized payload.
293
+ */
294
+ tokenize(auth, tenant, config, payload) {
295
+ return this.app.tokenize(auth, tenant, config, payload);
296
+ }
297
+ /**
298
+ * Detokenizes the given payload.
299
+ * @param tenant The tenant to detokenize the payload for.
300
+ * @param payload The payload to detokenize.
301
+ * @returns The detokenized payload.
302
+ */
303
+ detokenize(auth, tenant, config, payload) {
304
+ return this.app.detokenize(auth, tenant, config, payload);
305
+ }
306
+ };
307
+ export {
308
+ Tokenization
309
+ };
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@edirect/tokenization",
3
+ "version": "0.0.1",
4
+ "description": "Javascript library for tokenization service",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "scripts": {
12
+ "build": "tsup",
13
+ "dev": "tsup --watch",
14
+ "test": "vitest run",
15
+ "test:watch": "vitest",
16
+ "coverage": "vitest run --coverage"
17
+ },
18
+ "keywords": [
19
+ "typescript",
20
+ "library"
21
+ ],
22
+ "author": "Igor Quirino <igor.quirino@bolttech.com>",
23
+ "license": "MIT",
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "https://bitbucket.org/gofrank/tokenization-service-js.git"
27
+ },
28
+ "bugs": {
29
+ "url": "https://github.com/gofrank/tokenization-service-js/issues"
30
+ },
31
+ "homepage": "https://github.com/gofrank/tokenization-service-js#readme",
32
+ "devDependencies": {
33
+ "@types/node": "^22.10.1",
34
+ "@vitest/coverage-istanbul": "^2.1.8",
35
+ "tsup": "^8.3.5",
36
+ "typescript": "^5.7.2",
37
+ "vitest": "^2.1.8"
38
+ }
39
+ }