@thirdweb-dev/service-utils 0.2.5 → 0.3.0-nightly-d01e6396-20230802191417

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
@@ -1 +1,7 @@
1
- ### todo
1
+ # Service Utils
2
+
3
+ This repository contains utilities that handle common requirements for thirdweb services.
4
+
5
+ - `/cf-worker`: for use only in Cloudflare Worker services
6
+ - `/node`: for use in self-hosted services
7
+ - `/core`: for use in any service
@@ -0,0 +1,68 @@
1
+ import { z } from "zod";
2
+ import { AwsCredentialIdentity } from "@smithy/types";
3
+ /**
4
+ * Types
5
+ */
6
+ declare const usageEventSchema: z.ZodObject<{
7
+ source: z.ZodEnum<["wallet", "rpc", "storage", "bundler", "paymaster", "relayer"]>;
8
+ action: z.ZodString;
9
+ accountId: z.ZodString;
10
+ apiKeyId: z.ZodOptional<z.ZodString>;
11
+ creatorWalletAddress: z.ZodOptional<z.ZodString>;
12
+ clientId: z.ZodOptional<z.ZodString>;
13
+ walletAddress: z.ZodOptional<z.ZodString>;
14
+ chainId: z.ZodOptional<z.ZodNumber>;
15
+ provider: z.ZodOptional<z.ZodString>;
16
+ mimeType: z.ZodOptional<z.ZodString>;
17
+ fileSize: z.ZodOptional<z.ZodNumber>;
18
+ fileCid: z.ZodOptional<z.ZodString>;
19
+ transactionHash: z.ZodOptional<z.ZodString>;
20
+ gasLimit: z.ZodOptional<z.ZodNumber>;
21
+ gasPricePerUnit: z.ZodOptional<z.ZodNumber>;
22
+ }, "strip", z.ZodTypeAny, {
23
+ source: "storage" | "rpc" | "bundler" | "relayer" | "wallet" | "paymaster";
24
+ action: string;
25
+ accountId: string;
26
+ apiKeyId?: string | undefined;
27
+ creatorWalletAddress?: string | undefined;
28
+ clientId?: string | undefined;
29
+ walletAddress?: string | undefined;
30
+ chainId?: number | undefined;
31
+ provider?: string | undefined;
32
+ mimeType?: string | undefined;
33
+ fileSize?: number | undefined;
34
+ fileCid?: string | undefined;
35
+ transactionHash?: string | undefined;
36
+ gasLimit?: number | undefined;
37
+ gasPricePerUnit?: number | undefined;
38
+ }, {
39
+ source: "storage" | "rpc" | "bundler" | "relayer" | "wallet" | "paymaster";
40
+ action: string;
41
+ accountId: string;
42
+ apiKeyId?: string | undefined;
43
+ creatorWalletAddress?: string | undefined;
44
+ clientId?: string | undefined;
45
+ walletAddress?: string | undefined;
46
+ chainId?: number | undefined;
47
+ provider?: string | undefined;
48
+ mimeType?: string | undefined;
49
+ fileSize?: number | undefined;
50
+ fileCid?: string | undefined;
51
+ transactionHash?: string | undefined;
52
+ gasLimit?: number | undefined;
53
+ gasPricePerUnit?: number | undefined;
54
+ }>;
55
+ export type UsageEvent = z.infer<typeof usageEventSchema>;
56
+ /**
57
+ * Publish usage events. Provide the relevant fields for your application.
58
+ *
59
+ * @param usageEvents
60
+ * @param config
61
+ */
62
+ export declare function publishUsageEvents(usageEvents: UsageEvent[], config: {
63
+ queueUrl: string;
64
+ region?: string;
65
+ credentials?: AwsCredentialIdentity;
66
+ }): Promise<void>;
67
+ export {};
68
+ //# sourceMappingURL=usage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"usage.d.ts","sourceRoot":"../../../../src/core","sources":["usage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAMxB,OAAO,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAC;AAkBtD;;GAEG;AACH,QAAA,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAyBpB,CAAC;AACH,MAAM,MAAM,UAAU,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,gBAAgB,CAAC,CAAC;AAE1D;;;;;GAKG;AACH,wBAAsB,kBAAkB,CACtC,WAAW,EAAE,UAAU,EAAE,EACzB,MAAM,EAAE;IACN,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,qBAAqB,CAAC;CACrC,GACA,OAAO,CAAC,IAAI,CAAC,CAaf"}
@@ -1,2 +1,3 @@
1
1
  export * from "./core/services";
2
+ export * from "./core/usage";
2
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"../../../src","sources":["index.ts"],"names":[],"mappings":"AACA,cAAc,iBAAiB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"../../../src","sources":["index.ts"],"names":[],"mappings":"AACA,cAAc,iBAAiB,CAAC;AAGhC,cAAc,cAAc,CAAC"}
@@ -3,10 +3,74 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var services = require('./services-a3f36057.cjs.dev.js');
6
+ var zod = require('zod');
7
+ var clientSqs = require('@aws-sdk/client-sqs');
8
+ var crypto = require('crypto');
6
9
 
10
+ let sqs;
11
+ function getSqs(_ref) {
12
+ let {
13
+ region,
14
+ credentials
15
+ } = _ref;
16
+ if (!sqs) {
17
+ sqs = new clientSqs.SQSClient({
18
+ region,
19
+ credentials
20
+ });
21
+ }
22
+ return sqs;
23
+ }
7
24
 
25
+ /**
26
+ * Types
27
+ */
28
+ const usageEventSchema = zod.z.object({
29
+ source: zod.z.enum(["wallet", "rpc", "storage", "bundler", "paymaster", "relayer"]),
30
+ action: zod.z.string(),
31
+ accountId: zod.z.string(),
32
+ // Optional
33
+ apiKeyId: zod.z.string().optional(),
34
+ creatorWalletAddress: zod.z.string().optional(),
35
+ clientId: zod.z.string().optional(),
36
+ walletAddress: zod.z.string().optional(),
37
+ chainId: zod.z.number().int().positive().optional(),
38
+ provider: zod.z.string().optional(),
39
+ mimeType: zod.z.string().optional(),
40
+ fileSize: zod.z.number().int().nonnegative().optional(),
41
+ fileCid: zod.z.string().optional(),
42
+ transactionHash: zod.z.string().optional(),
43
+ gasLimit: zod.z.number().nonnegative().optional(),
44
+ gasPricePerUnit: zod.z.number().nonnegative().optional()
45
+ });
46
+ /**
47
+ * Publish usage events. Provide the relevant fields for your application.
48
+ *
49
+ * @param usageEvents
50
+ * @param config
51
+ */
52
+ async function publishUsageEvents(usageEvents, config) {
53
+ const {
54
+ queueUrl,
55
+ region = "us-west-2",
56
+ credentials
57
+ } = config;
58
+ const entries = usageEvents.map(event => ({
59
+ Id: crypto.randomUUID(),
60
+ MessageBody: JSON.stringify(usageEventSchema.parse(event))
61
+ }));
62
+ const input = new clientSqs.SendMessageBatchCommand({
63
+ QueueUrl: queueUrl,
64
+ Entries: entries
65
+ });
66
+ await getSqs({
67
+ region,
68
+ credentials
69
+ }).send(input);
70
+ }
8
71
 
9
72
  exports.SERVICES = services.SERVICES;
10
73
  exports.SERVICE_DEFINITIONS = services.SERVICE_DEFINITIONS;
11
74
  exports.SERVICE_NAMES = services.SERVICE_NAMES;
12
75
  exports.getServiceByName = services.getServiceByName;
76
+ exports.publishUsageEvents = publishUsageEvents;
@@ -3,10 +3,74 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var services = require('./services-9e185105.cjs.prod.js');
6
+ var zod = require('zod');
7
+ var clientSqs = require('@aws-sdk/client-sqs');
8
+ var crypto = require('crypto');
6
9
 
10
+ let sqs;
11
+ function getSqs(_ref) {
12
+ let {
13
+ region,
14
+ credentials
15
+ } = _ref;
16
+ if (!sqs) {
17
+ sqs = new clientSqs.SQSClient({
18
+ region,
19
+ credentials
20
+ });
21
+ }
22
+ return sqs;
23
+ }
7
24
 
25
+ /**
26
+ * Types
27
+ */
28
+ const usageEventSchema = zod.z.object({
29
+ source: zod.z.enum(["wallet", "rpc", "storage", "bundler", "paymaster", "relayer"]),
30
+ action: zod.z.string(),
31
+ accountId: zod.z.string(),
32
+ // Optional
33
+ apiKeyId: zod.z.string().optional(),
34
+ creatorWalletAddress: zod.z.string().optional(),
35
+ clientId: zod.z.string().optional(),
36
+ walletAddress: zod.z.string().optional(),
37
+ chainId: zod.z.number().int().positive().optional(),
38
+ provider: zod.z.string().optional(),
39
+ mimeType: zod.z.string().optional(),
40
+ fileSize: zod.z.number().int().nonnegative().optional(),
41
+ fileCid: zod.z.string().optional(),
42
+ transactionHash: zod.z.string().optional(),
43
+ gasLimit: zod.z.number().nonnegative().optional(),
44
+ gasPricePerUnit: zod.z.number().nonnegative().optional()
45
+ });
46
+ /**
47
+ * Publish usage events. Provide the relevant fields for your application.
48
+ *
49
+ * @param usageEvents
50
+ * @param config
51
+ */
52
+ async function publishUsageEvents(usageEvents, config) {
53
+ const {
54
+ queueUrl,
55
+ region = "us-west-2",
56
+ credentials
57
+ } = config;
58
+ const entries = usageEvents.map(event => ({
59
+ Id: crypto.randomUUID(),
60
+ MessageBody: JSON.stringify(usageEventSchema.parse(event))
61
+ }));
62
+ const input = new clientSqs.SendMessageBatchCommand({
63
+ QueueUrl: queueUrl,
64
+ Entries: entries
65
+ });
66
+ await getSqs({
67
+ region,
68
+ credentials
69
+ }).send(input);
70
+ }
8
71
 
9
72
  exports.SERVICES = services.SERVICES;
10
73
  exports.SERVICE_DEFINITIONS = services.SERVICE_DEFINITIONS;
11
74
  exports.SERVICE_NAMES = services.SERVICE_NAMES;
12
75
  exports.getServiceByName = services.getServiceByName;
76
+ exports.publishUsageEvents = publishUsageEvents;
@@ -1 +1,68 @@
1
1
  export { b as SERVICES, S as SERVICE_DEFINITIONS, a as SERVICE_NAMES, g as getServiceByName } from './services-86283509.esm.js';
2
+ import { z } from 'zod';
3
+ import { SendMessageBatchCommand, SQSClient } from '@aws-sdk/client-sqs';
4
+ import { randomUUID } from 'crypto';
5
+
6
+ let sqs;
7
+ function getSqs(_ref) {
8
+ let {
9
+ region,
10
+ credentials
11
+ } = _ref;
12
+ if (!sqs) {
13
+ sqs = new SQSClient({
14
+ region,
15
+ credentials
16
+ });
17
+ }
18
+ return sqs;
19
+ }
20
+
21
+ /**
22
+ * Types
23
+ */
24
+ const usageEventSchema = z.object({
25
+ source: z.enum(["wallet", "rpc", "storage", "bundler", "paymaster", "relayer"]),
26
+ action: z.string(),
27
+ accountId: z.string(),
28
+ // Optional
29
+ apiKeyId: z.string().optional(),
30
+ creatorWalletAddress: z.string().optional(),
31
+ clientId: z.string().optional(),
32
+ walletAddress: z.string().optional(),
33
+ chainId: z.number().int().positive().optional(),
34
+ provider: z.string().optional(),
35
+ mimeType: z.string().optional(),
36
+ fileSize: z.number().int().nonnegative().optional(),
37
+ fileCid: z.string().optional(),
38
+ transactionHash: z.string().optional(),
39
+ gasLimit: z.number().nonnegative().optional(),
40
+ gasPricePerUnit: z.number().nonnegative().optional()
41
+ });
42
+ /**
43
+ * Publish usage events. Provide the relevant fields for your application.
44
+ *
45
+ * @param usageEvents
46
+ * @param config
47
+ */
48
+ async function publishUsageEvents(usageEvents, config) {
49
+ const {
50
+ queueUrl,
51
+ region = "us-west-2",
52
+ credentials
53
+ } = config;
54
+ const entries = usageEvents.map(event => ({
55
+ Id: randomUUID(),
56
+ MessageBody: JSON.stringify(usageEventSchema.parse(event))
57
+ }));
58
+ const input = new SendMessageBatchCommand({
59
+ QueueUrl: queueUrl,
60
+ Entries: entries
61
+ });
62
+ await getSqs({
63
+ region,
64
+ credentials
65
+ }).send(input);
66
+ }
67
+
68
+ export { publishUsageEvents };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thirdweb-dev/service-utils",
3
- "version": "0.2.5",
3
+ "version": "0.3.0-nightly-d01e6396-20230802191417",
4
4
  "main": "dist/thirdweb-dev-service-utils.cjs.js",
5
5
  "module": "dist/thirdweb-dev-service-utils.esm.js",
6
6
  "exports": {
@@ -41,6 +41,7 @@
41
41
  "devDependencies": {
42
42
  "@cloudflare/workers-types": "^4.20230724.0",
43
43
  "@preconstruct/cli": "2.7.0",
44
+ "@smithy/types": "^2.0.2",
44
45
  "@thirdweb-dev/tsconfig": "^0.1.7",
45
46
  "@types/jest": "^29.5.3",
46
47
  "@types/node": "^18.17.1",
@@ -53,6 +54,10 @@
53
54
  "prettier": "^3.0.0",
54
55
  "typescript": "^5.1.6"
55
56
  },
57
+ "dependencies": {
58
+ "@aws-sdk/client-sqs": "^3.379.1",
59
+ "zod": "^3.20.2"
60
+ },
56
61
  "scripts": {
57
62
  "format": "prettier --write 'src/**/*'",
58
63
  "lint": "eslint src/",