@friggframework/api-module-frigg-scale-test 0.1.1-canary.49.1d97838.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/LICENSE.md +16 -0
- package/README.md +0 -0
- package/jest.config.js +9 -0
- package/package.json +26 -0
- package/src/api.ts +138 -0
- package/src/handler-proxy.d.ts +4 -0
- package/src/index.ts +27 -0
- package/src/types.d.ts +9 -0
- package/test/smoke.test.ts +117 -0
- package/tsconfig.json +17 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Left Hook Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
|
6
|
+
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
|
|
7
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
8
|
+
persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or
|
|
11
|
+
substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
|
|
14
|
+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
15
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
16
|
+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
File without changes
|
package/jest.config.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@friggframework/api-module-frigg-scale-test",
|
|
3
|
+
"version": "0.1.1-canary.49.1d97838.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc -p tsconfig.json",
|
|
9
|
+
"test": "jest --runInBand",
|
|
10
|
+
"lint": "eslint ."
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@friggframework/core": "^1.0.0"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@types/jest": "^29.5.0",
|
|
17
|
+
"@types/node": "^20.8.0",
|
|
18
|
+
"jest": "^29.7.0",
|
|
19
|
+
"ts-jest": "^29.1.0",
|
|
20
|
+
"typescript": "^5.4.0"
|
|
21
|
+
},
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"gitHead": "1d978381f7d76dc3cde518965e608c4db84062fa"
|
|
26
|
+
}
|
package/src/api.ts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
export type ListParams = {
|
|
2
|
+
accountId: string;
|
|
3
|
+
limit?: number;
|
|
4
|
+
cursor?: string;
|
|
5
|
+
updatedSince?: string;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export type ListActivitiesParams = ListParams & {
|
|
9
|
+
type?: "phone_call" | "email" | "sms";
|
|
10
|
+
contactId?: string;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export class FriggScaleTestAPI {
|
|
14
|
+
constructor(readonly opts: { baseUrl?: string; apiKey?: string } = {}) {}
|
|
15
|
+
|
|
16
|
+
private get base(): string {
|
|
17
|
+
return (
|
|
18
|
+
this.opts.baseUrl ||
|
|
19
|
+
process.env.FRIGG_SCALE_TEST_BASE_URL ||
|
|
20
|
+
"http://localhost:4000"
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
private headers(): Record<string, string> {
|
|
25
|
+
const apiKey = this.opts.apiKey || process.env.FRIGG_SCALE_TEST_API_KEY;
|
|
26
|
+
return apiKey ? { Authorization: `Bearer ${apiKey}` } : {};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async health() {
|
|
30
|
+
const r = await fetch(new URL("/health", this.base));
|
|
31
|
+
if (!r.ok) throw new Error(`health ${r.status}`);
|
|
32
|
+
return r.json();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async getConfig(accountId: string) {
|
|
36
|
+
const r = await fetch(new URL(`/config/${encodeURIComponent(accountId)}`, this.base), {
|
|
37
|
+
headers: this.headers()
|
|
38
|
+
});
|
|
39
|
+
if (!r.ok) throw new Error(`getConfig ${r.status}`);
|
|
40
|
+
return r.json();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async putConfig(accountId: string, cfg: any) {
|
|
44
|
+
const headers: Record<string, string> = {
|
|
45
|
+
"content-type": "application/json",
|
|
46
|
+
...this.headers()
|
|
47
|
+
};
|
|
48
|
+
const r = await fetch(new URL(`/config/${encodeURIComponent(accountId)}`, this.base), {
|
|
49
|
+
method: "PUT",
|
|
50
|
+
headers,
|
|
51
|
+
body: JSON.stringify(cfg)
|
|
52
|
+
});
|
|
53
|
+
if (!r.ok) throw new Error(`putConfig ${r.status}`);
|
|
54
|
+
return r.json();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async listContacts(params: ListParams) {
|
|
58
|
+
const url = new URL(`/contacts`, this.base);
|
|
59
|
+
url.searchParams.set("accountId", params.accountId);
|
|
60
|
+
if (params.limit) url.searchParams.set("limit", String(params.limit));
|
|
61
|
+
if (params.cursor) url.searchParams.set("cursor", params.cursor);
|
|
62
|
+
if (params.updatedSince) url.searchParams.set("updatedSince", params.updatedSince);
|
|
63
|
+
const r = await fetch(url, { headers: this.headers() });
|
|
64
|
+
if (!r.ok) throw new Error(`listContacts ${r.status}`);
|
|
65
|
+
return r.json();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async listActivities(params: ListActivitiesParams) {
|
|
69
|
+
const url = new URL(`/activities`, this.base);
|
|
70
|
+
url.searchParams.set("accountId", params.accountId);
|
|
71
|
+
if (params.limit) url.searchParams.set("limit", String(params.limit));
|
|
72
|
+
if (params.cursor) url.searchParams.set("cursor", params.cursor);
|
|
73
|
+
if (params.updatedSince) url.searchParams.set("updatedSince", params.updatedSince);
|
|
74
|
+
if (params.type) url.searchParams.set("type", params.type);
|
|
75
|
+
if (params.contactId) url.searchParams.set("contactId", params.contactId);
|
|
76
|
+
const r = await fetch(url, { headers: this.headers() });
|
|
77
|
+
if (!r.ok) throw new Error(`listActivities ${r.status}`);
|
|
78
|
+
return r.json();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async createActivity(body: any) {
|
|
82
|
+
const headers: Record<string, string> = {
|
|
83
|
+
"content-type": "application/json",
|
|
84
|
+
...this.headers()
|
|
85
|
+
};
|
|
86
|
+
const r = await fetch(new URL(`/activities`, this.base), {
|
|
87
|
+
method: "POST",
|
|
88
|
+
headers,
|
|
89
|
+
body: JSON.stringify(body)
|
|
90
|
+
});
|
|
91
|
+
if (!r.ok) throw new Error(`createActivity ${r.status}`);
|
|
92
|
+
return r.json();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async requestContactsExport(body: {
|
|
96
|
+
accountId: string;
|
|
97
|
+
format?: "ndjson" | "csv";
|
|
98
|
+
fields?: string[];
|
|
99
|
+
}) {
|
|
100
|
+
const headers: Record<string, string> = {
|
|
101
|
+
"content-type": "application/json",
|
|
102
|
+
...this.headers()
|
|
103
|
+
};
|
|
104
|
+
const r = await fetch(new URL(`/bulk/exports/contacts`, this.base), {
|
|
105
|
+
method: "POST",
|
|
106
|
+
headers,
|
|
107
|
+
body: JSON.stringify(body)
|
|
108
|
+
});
|
|
109
|
+
if (r.status !== 202 && r.status !== 200) throw new Error(`requestContactsExport ${r.status}`);
|
|
110
|
+
return r.json();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async requestActivitiesExport(body: {
|
|
114
|
+
accountId: string;
|
|
115
|
+
format?: "ndjson" | "csv";
|
|
116
|
+
fields?: string[];
|
|
117
|
+
}) {
|
|
118
|
+
const headers: Record<string, string> = {
|
|
119
|
+
"content-type": "application/json",
|
|
120
|
+
...this.headers()
|
|
121
|
+
};
|
|
122
|
+
const r = await fetch(new URL(`/bulk/exports/activities`, this.base), {
|
|
123
|
+
method: "POST",
|
|
124
|
+
headers,
|
|
125
|
+
body: JSON.stringify(body)
|
|
126
|
+
});
|
|
127
|
+
if (r.status !== 202 && r.status !== 200) throw new Error(`requestActivitiesExport ${r.status}`);
|
|
128
|
+
return r.json();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async getExportJob(jobId: string) {
|
|
132
|
+
const r = await fetch(new URL(`/bulk/exports/${encodeURIComponent(jobId)}`, this.base), {
|
|
133
|
+
headers: this.headers()
|
|
134
|
+
});
|
|
135
|
+
if (!r.ok) throw new Error(`getExportJob ${r.status}`);
|
|
136
|
+
return r.json();
|
|
137
|
+
}
|
|
138
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { FriggModuleAuthDefinition } from "@friggframework/core";
|
|
2
|
+
import { FriggScaleTestAPI } from "./api";
|
|
3
|
+
|
|
4
|
+
export const authDef: FriggModuleAuthDefinition = {
|
|
5
|
+
API: FriggScaleTestAPI,
|
|
6
|
+
getName: () => "Frigg Scale Test (Mock CRM)",
|
|
7
|
+
moduleName: "frigg-scale-test",
|
|
8
|
+
requiredAuthMethods: {
|
|
9
|
+
apiPropertiesToPersist: { credential: ["apiKey"], entity: [] },
|
|
10
|
+
getToken: async () => undefined,
|
|
11
|
+
getEntityDetails: async (_api: FriggScaleTestAPI, params?: any) => ({
|
|
12
|
+
identifiers: { externalId: `frigg-scale-test:${params?.accountId || "default"}` },
|
|
13
|
+
details: { name: "Frigg Scale Test Mock CRM" }
|
|
14
|
+
}),
|
|
15
|
+
getCredentialDetails: async () => ({ identifiers: { externalId: "frigg-scale-test-cred" }, details: {} }),
|
|
16
|
+
testAuthRequest: async (api: FriggScaleTestAPI) => {
|
|
17
|
+
await api.health();
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
env: {
|
|
21
|
+
FRIGG_SCALE_TEST_BASE_URL: process.env.FRIGG_SCALE_TEST_BASE_URL,
|
|
22
|
+
FRIGG_SCALE_TEST_API_KEY: process.env.FRIGG_SCALE_TEST_API_KEY
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export { FriggScaleTestAPI };
|
|
27
|
+
export default authDef;
|
package/src/types.d.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { createServer, IncomingMessage, ServerResponse } from "http";
|
|
2
|
+
import { AddressInfo } from "net";
|
|
3
|
+
import { handler } from "services/frigg-scale-test-lambda/src/handler";
|
|
4
|
+
import { FriggScaleTestAPI } from "../src/api";
|
|
5
|
+
import { APIGatewayProxyEventV2 } from "aws-lambda";
|
|
6
|
+
|
|
7
|
+
let server: ReturnType<typeof createServer>;
|
|
8
|
+
let baseUrl: string;
|
|
9
|
+
|
|
10
|
+
function buildEvent(req: IncomingMessage, body: string): APIGatewayProxyEventV2 {
|
|
11
|
+
const url = new URL(req.url || "/", "http://localhost");
|
|
12
|
+
const headers: Record<string, string> = {};
|
|
13
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
14
|
+
if (typeof value === "string") headers[key] = value;
|
|
15
|
+
if (Array.isArray(value)) headers[key] = value.join(",");
|
|
16
|
+
}
|
|
17
|
+
const query: Record<string, string> = {};
|
|
18
|
+
url.searchParams.forEach((value, key) => {
|
|
19
|
+
query[key] = value;
|
|
20
|
+
});
|
|
21
|
+
return {
|
|
22
|
+
version: "2.0",
|
|
23
|
+
routeKey: "$default",
|
|
24
|
+
rawPath: url.pathname,
|
|
25
|
+
rawQueryString: url.search.slice(1),
|
|
26
|
+
headers,
|
|
27
|
+
queryStringParameters: Object.keys(query).length ? query : null,
|
|
28
|
+
requestContext: {
|
|
29
|
+
accountId: "local",
|
|
30
|
+
apiId: "local",
|
|
31
|
+
domainName: "localhost",
|
|
32
|
+
domainPrefix: "",
|
|
33
|
+
requestId: "local",
|
|
34
|
+
routeKey: "$default",
|
|
35
|
+
stage: "$default",
|
|
36
|
+
time: new Date().toISOString(),
|
|
37
|
+
timeEpoch: Date.now(),
|
|
38
|
+
http: {
|
|
39
|
+
method: req.method || "GET",
|
|
40
|
+
path: url.pathname,
|
|
41
|
+
protocol: "HTTP/1.1",
|
|
42
|
+
sourceIp: "127.0.0.1",
|
|
43
|
+
userAgent: "jest"
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
isBase64Encoded: false,
|
|
47
|
+
body: body || undefined,
|
|
48
|
+
pathParameters: null,
|
|
49
|
+
stageVariables: null,
|
|
50
|
+
cookies: [],
|
|
51
|
+
multiValueQueryStringParameters: null
|
|
52
|
+
} as unknown as APIGatewayProxyEventV2;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function ensureStructured(result: Awaited<ReturnType<typeof handler>>) {
|
|
56
|
+
if (typeof result === "string") {
|
|
57
|
+
return { statusCode: 200, headers: {}, body: result } as const;
|
|
58
|
+
}
|
|
59
|
+
return result;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
beforeAll(async () => {
|
|
63
|
+
server = createServer((req: IncomingMessage, res: ServerResponse) => {
|
|
64
|
+
const chunks: Buffer[] = [];
|
|
65
|
+
req.on("data", (chunk: Buffer) => chunks.push(chunk));
|
|
66
|
+
req.on("end", async () => {
|
|
67
|
+
const body = Buffer.concat(chunks).toString();
|
|
68
|
+
try {
|
|
69
|
+
const event = buildEvent(req, body);
|
|
70
|
+
const result = ensureStructured(await handler(event));
|
|
71
|
+
res.statusCode = result.statusCode || 200;
|
|
72
|
+
for (const [key, value] of Object.entries(result.headers || {})) {
|
|
73
|
+
if (value !== undefined) {
|
|
74
|
+
res.setHeader(key, value as string);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
res.end(result.body || "");
|
|
78
|
+
} catch (err: any) {
|
|
79
|
+
res.statusCode = 500;
|
|
80
|
+
res.end(JSON.stringify({ error: err?.message || "error" }));
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
await new Promise<void>((resolve) => {
|
|
86
|
+
server.listen(0, () => resolve());
|
|
87
|
+
});
|
|
88
|
+
const address = server.address() as AddressInfo;
|
|
89
|
+
baseUrl = `http://127.0.0.1:${address.port}`;
|
|
90
|
+
process.env.FRIGG_SCALE_TEST_BASE_URL = baseUrl;
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
afterAll(async () => {
|
|
94
|
+
await new Promise<void>((resolve) => server.close(() => resolve()));
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
const api = new FriggScaleTestAPI();
|
|
98
|
+
|
|
99
|
+
describe("Frigg Scale Test Mock CRM", () => {
|
|
100
|
+
it("health", async () => {
|
|
101
|
+
const health = await api.health();
|
|
102
|
+
expect(health.ok).toBeTruthy();
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("contacts pagination", async () => {
|
|
106
|
+
const page = await api.listContacts({ accountId: "demo", limit: 10 });
|
|
107
|
+
expect(Array.isArray(page.items)).toBe(true);
|
|
108
|
+
expect(page.items.length).toBeLessThanOrEqual(10);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("activities list and create", async () => {
|
|
112
|
+
const list = await api.listActivities({ accountId: "demo", limit: 5, type: "email" });
|
|
113
|
+
expect(Array.isArray(list.items)).toBe(true);
|
|
114
|
+
const created = await api.createActivity({ accountId: "demo", type: "sms", contactId: "contact-1", subject: "Ping" });
|
|
115
|
+
expect(created.id).toBeTruthy();
|
|
116
|
+
});
|
|
117
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"outDir": "dist",
|
|
4
|
+
"rootDir": "src",
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"module": "commonjs",
|
|
7
|
+
"target": "ES2020",
|
|
8
|
+
"esModuleInterop": true,
|
|
9
|
+
"strict": true,
|
|
10
|
+
"baseUrl": ".",
|
|
11
|
+
"paths": {
|
|
12
|
+
"services/*": ["../../../services/*"]
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"include": ["src/**/*"],
|
|
16
|
+
"exclude": ["dist", "test"]
|
|
17
|
+
}
|