@teamkeel/testing-runtime 0.351.0 → 0.353.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teamkeel/testing-runtime",
3
- "version": "0.351.0",
3
+ "version": "0.353.0",
4
4
  "description": "Internal package used by the generated @teamkeel/testing package",
5
5
  "exports": "./src/index.mjs",
6
6
  "typings": "src/index.d.ts",
@@ -0,0 +1,88 @@
1
+ import jwt from "jsonwebtoken";
2
+
3
+ export class JobExecutor {
4
+ constructor(props) {
5
+ this._identity = props.identity || null;
6
+ this._authToken = props.authToken || null;
7
+
8
+ // Return a proxy which will return a bound version of the
9
+ // _execute method for any unknown properties. This creates
10
+ // the jobs API we want but in a dynamic way without needing
11
+ // codegen. We then generate the right type definitions for
12
+ // this class in the @teamkeel/testing package.
13
+ return new Proxy(this, {
14
+ get(target, prop) {
15
+ const v = Reflect.get(...arguments);
16
+ if (v !== undefined) {
17
+ return v;
18
+ }
19
+ return target._execute.bind(target, prop);
20
+ },
21
+ });
22
+ }
23
+ withIdentity(i) {
24
+ return new JobExecutor({ identity: i });
25
+ }
26
+ withAuthToken(t) {
27
+ return new JobExecutor({ authToken: t });
28
+ }
29
+ _execute(method, params) {
30
+ const headers = { "Content-Type": "application/json" };
31
+
32
+ // An Identity instance is provided make a JWT
33
+ if (this._identity !== null) {
34
+ headers["Authorization"] =
35
+ "Bearer " +
36
+ jwt.sign(
37
+ {},
38
+ // Not using a signing algorithm, therefore the private key is undefined
39
+ undefined,
40
+ {
41
+ algorithm: "none",
42
+ expiresIn: 60 * 60 * 24,
43
+ subject: this._identity.id,
44
+ issuer: "keel",
45
+ }
46
+ );
47
+ }
48
+
49
+ // If an auth token is provided that can be sent as-is
50
+ if (this._authToken !== null) {
51
+ headers["Authorization"] = "Bearer " + this._authToken;
52
+ }
53
+ return fetch(process.env.KEEL_TESTING_JOBS_URL + "/" + method, {
54
+ method: "POST",
55
+ body: JSON.stringify(params),
56
+ headers,
57
+ }).then((r) => {
58
+ if (r.status !== 200) {
59
+ // For non-200 first read the response as text
60
+ return r.text().then((t) => {
61
+ let d;
62
+
63
+ try {
64
+ d = JSON.parse(t);
65
+ } catch (e) {
66
+ if ("DEBUG" in process.env) {
67
+ console.log(e);
68
+ }
69
+ // If JSON parsing fails then throw an error with the
70
+ // response text as the message
71
+ throw new Error(t);
72
+ }
73
+ // Otherwise throw the parsed JSON error response
74
+ // We override toString as otherwise you get expect errors like:
75
+ // `expected to resolve but rejected with "[object Object]"`
76
+ Object.defineProperty(d, "toString", {
77
+ value: () => t,
78
+ enumerable: false,
79
+ });
80
+
81
+ throw d;
82
+ });
83
+ }
84
+
85
+ return true;
86
+ });
87
+ }
88
+ }
package/src/index.mjs CHANGED
@@ -1,4 +1,5 @@
1
1
  export { sql } from "kysely";
2
2
  export { ActionExecutor } from "./ActionExecutor.mjs";
3
+ export { JobExecutor } from "./JobExecutor.mjs";
3
4
  export { toHaveError } from "./toHaveError.mjs";
4
5
  export { toHaveAuthorizationError } from "./toHaveAuthorizationError.mjs";