@scrawn/core 0.0.2 → 0.0.6
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 +40 -0
- package/dist/config.d.ts +17 -2
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +9 -2
- package/dist/config.js.map +1 -1
- package/dist/core/auth/apiKeyAuth.d.ts +4 -13
- package/dist/core/auth/apiKeyAuth.d.ts.map +1 -1
- package/dist/core/auth/apiKeyAuth.js +12 -17
- package/dist/core/auth/apiKeyAuth.js.map +1 -1
- package/dist/core/auth/baseAuth.d.ts +14 -36
- package/dist/core/auth/baseAuth.d.ts.map +1 -1
- package/dist/core/auth/baseAuth.js +0 -6
- package/dist/core/auth/baseAuth.js.map +1 -1
- package/dist/core/errors/index.d.ts +192 -0
- package/dist/core/errors/index.d.ts.map +1 -0
- package/dist/core/errors/index.js +280 -0
- package/dist/core/errors/index.js.map +1 -0
- package/dist/core/grpc/callContext.d.ts +18 -0
- package/dist/core/grpc/callContext.d.ts.map +1 -0
- package/dist/core/grpc/callContext.js +35 -0
- package/dist/core/grpc/callContext.js.map +1 -0
- package/dist/core/grpc/client.d.ts +13 -123
- package/dist/core/grpc/client.d.ts.map +1 -1
- package/dist/core/grpc/client.js +23 -131
- package/dist/core/grpc/client.js.map +1 -1
- package/dist/core/grpc/index.d.ts +5 -3
- package/dist/core/grpc/index.d.ts.map +1 -1
- package/dist/core/grpc/index.js +4 -2
- package/dist/core/grpc/index.js.map +1 -1
- package/dist/core/grpc/requestBuilder.d.ts +12 -113
- package/dist/core/grpc/requestBuilder.d.ts.map +1 -1
- package/dist/core/grpc/requestBuilder.js +36 -126
- package/dist/core/grpc/requestBuilder.js.map +1 -1
- package/dist/core/grpc/streamRequestBuilder.d.ts +13 -0
- package/dist/core/grpc/streamRequestBuilder.d.ts.map +1 -0
- package/dist/core/grpc/streamRequestBuilder.js +60 -0
- package/dist/core/grpc/streamRequestBuilder.js.map +1 -0
- package/dist/core/grpc/types.d.ts +5 -52
- package/dist/core/grpc/types.d.ts.map +1 -1
- package/dist/core/grpc/types.js +0 -7
- package/dist/core/grpc/types.js.map +1 -1
- package/dist/core/pricing/builders.d.ts +157 -0
- package/dist/core/pricing/builders.d.ts.map +1 -0
- package/dist/core/pricing/builders.js +218 -0
- package/dist/core/pricing/builders.js.map +1 -0
- package/dist/core/pricing/index.d.ts +30 -0
- package/dist/core/pricing/index.d.ts.map +1 -0
- package/dist/core/pricing/index.js +32 -0
- package/dist/core/pricing/index.js.map +1 -0
- package/dist/core/pricing/resolve.d.ts +39 -0
- package/dist/core/pricing/resolve.d.ts.map +1 -0
- package/dist/core/pricing/resolve.js +50 -0
- package/dist/core/pricing/resolve.js.map +1 -0
- package/dist/core/pricing/serialize.d.ts +55 -0
- package/dist/core/pricing/serialize.d.ts.map +1 -0
- package/dist/core/pricing/serialize.js +127 -0
- package/dist/core/pricing/serialize.js.map +1 -0
- package/dist/core/pricing/types.d.ts +122 -0
- package/dist/core/pricing/types.d.ts.map +1 -0
- package/dist/core/pricing/types.js +17 -0
- package/dist/core/pricing/types.js.map +1 -0
- package/dist/core/pricing/validate.d.ts +56 -0
- package/dist/core/pricing/validate.d.ts.map +1 -0
- package/dist/core/pricing/validate.js +162 -0
- package/dist/core/pricing/validate.js.map +1 -0
- package/dist/core/scrawn.d.ts +218 -17
- package/dist/core/scrawn.d.ts.map +1 -1
- package/dist/core/scrawn.js +469 -71
- package/dist/core/scrawn.js.map +1 -1
- package/dist/core/types/auth.d.ts +1 -1
- package/dist/core/types/event.d.ts +182 -18
- package/dist/core/types/event.d.ts.map +1 -1
- package/dist/core/types/event.js +133 -5
- package/dist/core/types/event.js.map +1 -1
- package/dist/gen/auth/v1/auth_grpc_pb.d.ts +3 -0
- package/dist/gen/auth/v1/auth_grpc_pb.js +45 -0
- package/dist/gen/auth/v1/auth_pb.d.ts +63 -57
- package/dist/gen/auth/v1/auth_pb.js +471 -86
- package/dist/gen/data/v1/data_grpc_pb.d.ts +5 -0
- package/dist/gen/data/v1/data_grpc_pb.js +44 -0
- package/dist/gen/data/v1/data_pb.d.ts +254 -0
- package/dist/gen/data/v1/data_pb.js +1530 -0
- package/dist/gen/event/v1/event_grpc_pb.d.ts +3 -0
- package/dist/gen/event/v1/event_grpc_pb.js +79 -0
- package/dist/gen/event/v1/event_pb.d.ts +273 -100
- package/dist/gen/event/v1/event_pb.js +1862 -138
- package/dist/gen/package.json +3 -0
- package/dist/gen/payment/v1/payment_grpc_pb.d.ts +3 -0
- package/dist/gen/payment/v1/payment_grpc_pb.js +45 -0
- package/dist/gen/payment/v1/payment_pb.d.ts +43 -35
- package/dist/gen/payment/v1/payment_pb.js +321 -59
- package/dist/gen/query/v1/query_grpc_pb.d.ts +5 -0
- package/dist/gen/query/v1/query_grpc_pb.js +44 -0
- package/dist/gen/query/v1/query_pb.d.ts +359 -0
- package/dist/gen/query/v1/query_pb.js +2327 -0
- package/dist/index.d.ts +19 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +20 -10
- package/dist/index.js.map +1 -1
- package/dist/utils/forkAsyncIterable.d.ts +13 -0
- package/dist/utils/forkAsyncIterable.d.ts.map +1 -0
- package/dist/utils/forkAsyncIterable.js +78 -0
- package/dist/utils/forkAsyncIterable.js.map +1 -0
- package/dist/utils/logger.d.ts.map +1 -1
- package/dist/utils/logger.js +19 -19
- package/dist/utils/logger.js.map +1 -1
- package/dist/utils/pathMatcher.js +5 -5
- package/package.json +19 -15
- package/dist/gen/auth/v1/auth_connect.d.ts +0 -22
- package/dist/gen/auth/v1/auth_connect.d.ts.map +0 -1
- package/dist/gen/auth/v1/auth_connect.js +0 -26
- package/dist/gen/auth/v1/auth_connect.js.map +0 -1
- package/dist/gen/auth/v1/auth_pb.d.ts.map +0 -1
- package/dist/gen/auth/v1/auth_pb.js.map +0 -1
- package/dist/gen/event/v1/event_connect.d.ts +0 -22
- package/dist/gen/event/v1/event_connect.d.ts.map +0 -1
- package/dist/gen/event/v1/event_connect.js +0 -26
- package/dist/gen/event/v1/event_connect.js.map +0 -1
- package/dist/gen/event/v1/event_pb.d.ts.map +0 -1
- package/dist/gen/event/v1/event_pb.js.map +0 -1
- package/dist/gen/payment/v1/payment_connect.d.ts +0 -22
- package/dist/gen/payment/v1/payment_connect.d.ts.map +0 -1
- package/dist/gen/payment/v1/payment_connect.js +0 -26
- package/dist/gen/payment/v1/payment_connect.js.map +0 -1
- package/dist/gen/payment/v1/payment_pb.d.ts.map +0 -1
- package/dist/gen/payment/v1/payment_pb.js.map +0 -1
package/README.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# Scrawn SDK
|
|
2
|
+
|
|
3
|
+
## What is Scrawn.js?
|
|
4
|
+
|
|
5
|
+
Scrawn.js is the official TypeScript SDK for integrating Scrawn's usage-based billing into your applications. It provides a simple, type-safe interface for tracking usage events and collecting payments via gRPC.
|
|
6
|
+
|
|
7
|
+
[View Docs](https://scrawn.vercel.app/docs/scrawn-js)
|
|
8
|
+
|
|
9
|
+
## Key Features
|
|
10
|
+
|
|
11
|
+
- **Simple API** - Track usage with a single function call
|
|
12
|
+
- **Type-Safe** - Full TypeScript support with auto-completion
|
|
13
|
+
- **gRPC-Powered** - Built on Connect-RPC for efficient communication
|
|
14
|
+
- **Framework Agnostic** - Works with any JavaScript framework
|
|
15
|
+
- **Middleware Support** - Built-in middleware with whitelist/blacklist patterns
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
Install Scrawn.js in your project:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
bun add @scrawn/core
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Quick Example
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { Scrawn } from "@scrawn/core";
|
|
29
|
+
|
|
30
|
+
const scrawn = new Scrawn({
|
|
31
|
+
apiKey: process.env.SCRAWN_KEY as `scrn_${string}`,
|
|
32
|
+
baseURL: process.env.SCRAWN_BASE_URL || "http://localhost:8069",
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// Track a billable event
|
|
36
|
+
await scrawn.sdkCallEventConsumer({
|
|
37
|
+
userId: "user-123",
|
|
38
|
+
debitAmount: 100,
|
|
39
|
+
});
|
|
40
|
+
```
|
package/dist/config.d.ts
CHANGED
|
@@ -8,9 +8,9 @@ export declare const ScrawnConfig: {
|
|
|
8
8
|
*/
|
|
9
9
|
readonly grpc: {
|
|
10
10
|
/**
|
|
11
|
-
*
|
|
11
|
+
* Default gRPC port
|
|
12
12
|
*/
|
|
13
|
-
readonly
|
|
13
|
+
readonly defaultPort: 8069;
|
|
14
14
|
};
|
|
15
15
|
/**
|
|
16
16
|
* Logging configuration
|
|
@@ -23,4 +23,19 @@ export declare const ScrawnConfig: {
|
|
|
23
23
|
};
|
|
24
24
|
};
|
|
25
25
|
export type ScrawnConfig = typeof ScrawnConfig;
|
|
26
|
+
/**
|
|
27
|
+
* Configuration for the Scrawn CLI (`scrawn tag sync`).
|
|
28
|
+
* Used in the project's `scrawn.config.ts` file.
|
|
29
|
+
*/
|
|
30
|
+
export interface ScrawnCLIConfig {
|
|
31
|
+
apiKey: string;
|
|
32
|
+
grpcUrl: string;
|
|
33
|
+
httpUrl: string;
|
|
34
|
+
directory: string;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Type-safe helper for defining project configuration in `scrawn.config.ts`.
|
|
38
|
+
* Returns the config unchanged — purely a type-witness.
|
|
39
|
+
*/
|
|
40
|
+
export declare function scrawnConfig(config: ScrawnCLIConfig): ScrawnCLIConfig;
|
|
26
41
|
//# sourceMappingURL=config.d.ts.map
|
package/dist/config.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,eAAO,MAAM,YAAY;IACvB;;OAEG;;QAED;;WAEG;;;
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,eAAO,MAAM,YAAY;IACvB;;OAEG;;QAED;;WAEG;;;IAIL;;OAEG;;QAED;;WAEG;;;CAGG,CAAC;AAEX,MAAM,MAAM,YAAY,GAAG,OAAO,YAAY,CAAC;AAE/C;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC9B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,eAAe,GAAG,eAAe,CAErE"}
|
package/dist/config.js
CHANGED
|
@@ -8,9 +8,9 @@ export const ScrawnConfig = {
|
|
|
8
8
|
*/
|
|
9
9
|
grpc: {
|
|
10
10
|
/**
|
|
11
|
-
*
|
|
11
|
+
* Default gRPC port
|
|
12
12
|
*/
|
|
13
|
-
|
|
13
|
+
defaultPort: 8069,
|
|
14
14
|
},
|
|
15
15
|
/**
|
|
16
16
|
* Logging configuration
|
|
@@ -22,4 +22,11 @@ export const ScrawnConfig = {
|
|
|
22
22
|
enableDebug: false,
|
|
23
23
|
},
|
|
24
24
|
};
|
|
25
|
+
/**
|
|
26
|
+
* Type-safe helper for defining project configuration in `scrawn.config.ts`.
|
|
27
|
+
* Returns the config unchanged — purely a type-witness.
|
|
28
|
+
*/
|
|
29
|
+
export function scrawnConfig(config) {
|
|
30
|
+
return config;
|
|
31
|
+
}
|
|
25
32
|
//# sourceMappingURL=config.js.map
|
package/dist/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,CAAC,MAAM,YAAY,GAAG;IAC1B;;OAEG;IACH,IAAI,EAAE;QACJ;;WAEG;QACH,WAAW,EAAE,
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,CAAC,MAAM,YAAY,GAAG;IAC1B;;OAEG;IACH,IAAI,EAAE;QACJ;;WAEG;QACH,WAAW,EAAE,IAAI;KAClB;IAED;;OAEG;IACH,OAAO,EAAE;QACP;;WAEG;QACH,WAAW,EAAE,KAAK;KACnB;CACO,CAAC;AAeX;;;GAGG;AACH,MAAM,UAAU,YAAY,CAAC,MAAuB;IAClD,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { AuthBase } from
|
|
2
|
-
import type { Scrawn } from '../scrawn.js';
|
|
1
|
+
import { AuthBase } from "./baseAuth.js";
|
|
3
2
|
/**
|
|
4
|
-
* API key format:
|
|
5
|
-
*
|
|
3
|
+
* API key format: scrn_<role>_ followed by 32 alphanumeric characters.
|
|
4
|
+
* Roles: dash (dashboard), live (production), test
|
|
5
|
+
* @example 'scrn_live_abc123def456ghi789jkl012mno345pq'
|
|
6
6
|
*/
|
|
7
7
|
export type ApiKeyFormat = `scrn_${string}`;
|
|
8
8
|
/**
|
|
@@ -33,8 +33,6 @@ export type ApiKeyAuthCreds = {
|
|
|
33
33
|
* ```
|
|
34
34
|
*/
|
|
35
35
|
export declare class ApiKeyAuth extends AuthBase<ApiKeyAuthCreds> {
|
|
36
|
-
/** Authentication method identifier */
|
|
37
|
-
name: "api";
|
|
38
36
|
/** Validated API key */
|
|
39
37
|
private validatedKey;
|
|
40
38
|
/**
|
|
@@ -44,13 +42,6 @@ export declare class ApiKeyAuth extends AuthBase<ApiKeyAuthCreds> {
|
|
|
44
42
|
* @throws Error if API key format is invalid
|
|
45
43
|
*/
|
|
46
44
|
constructor(apiKey: string);
|
|
47
|
-
/**
|
|
48
|
-
* Initialize the API key auth method.
|
|
49
|
-
* No additional setup is required for API key authentication.
|
|
50
|
-
*
|
|
51
|
-
* @param scrawn - The Scrawn SDK instance (optional, unused)
|
|
52
|
-
*/
|
|
53
|
-
init(scrawn?: Scrawn): Promise<void>;
|
|
54
45
|
/**
|
|
55
46
|
* Get the API key credentials.
|
|
56
47
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"apiKeyAuth.d.ts","sourceRoot":"","sources":["../../../src/core/auth/apiKeyAuth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"apiKeyAuth.d.ts","sourceRoot":"","sources":["../../../src/core/auth/apiKeyAuth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAMzC;;;;GAIG;AACH,MAAM,MAAM,YAAY,GAAG,QAAQ,MAAM,EAAE,CAAC;AAI5C;;GAEG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,IAAI,YAAY,CAE9D;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,YAAY,CAcxD;AAED;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG;IAC5B,MAAM,EAAE,YAAY,CAAC;CACtB,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,qBAAa,UAAW,SAAQ,QAAQ,CAAC,eAAe,CAAC;IACvD,wBAAwB;IACxB,OAAO,CAAC,YAAY,CAAe;IAEnC;;;;;OAKG;gBACS,MAAM,EAAE,MAAM;IAK1B;;;;;;;;;;OAUG;IACG,QAAQ,IAAI,OAAO,CAAC,eAAe,CAAC;CAG3C"}
|
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import { AuthBase } from
|
|
2
|
-
import { ScrawnLogger } from
|
|
3
|
-
|
|
1
|
+
import { AuthBase } from "./baseAuth.js";
|
|
2
|
+
import { ScrawnLogger } from "../../utils/logger.js";
|
|
3
|
+
import { ScrawnValidationError } from "../errors/index.js";
|
|
4
|
+
const log = new ScrawnLogger("Auth");
|
|
5
|
+
const API_KEY_REGEX = /^scrn_(dash|live|test)_[a-zA-Z0-9]{32}$/;
|
|
4
6
|
/**
|
|
5
7
|
* Type guard to validate API key format
|
|
6
8
|
*/
|
|
7
9
|
export function isValidApiKey(key) {
|
|
8
|
-
return
|
|
10
|
+
return API_KEY_REGEX.test(key);
|
|
9
11
|
}
|
|
10
12
|
/**
|
|
11
13
|
* Validates and returns a properly typed API key
|
|
@@ -14,7 +16,12 @@ export function isValidApiKey(key) {
|
|
|
14
16
|
export function validateApiKey(key) {
|
|
15
17
|
if (!isValidApiKey(key)) {
|
|
16
18
|
log.error(`Invalid API key format: "${key}".`);
|
|
17
|
-
throw new
|
|
19
|
+
throw new ScrawnValidationError("Invalid API key format. Expected format: scrn_<dash|live|test>_<32 alphanumeric characters>", {
|
|
20
|
+
details: {
|
|
21
|
+
providedKey: key.substring(0, 14) + "...",
|
|
22
|
+
expectedFormat: "scrn_<role>_<32 alphanumeric characters>",
|
|
23
|
+
},
|
|
24
|
+
});
|
|
18
25
|
}
|
|
19
26
|
return key;
|
|
20
27
|
}
|
|
@@ -39,20 +46,8 @@ export class ApiKeyAuth extends AuthBase {
|
|
|
39
46
|
*/
|
|
40
47
|
constructor(apiKey) {
|
|
41
48
|
super();
|
|
42
|
-
/** Authentication method identifier */
|
|
43
|
-
this.name = 'api';
|
|
44
49
|
this.validatedKey = validateApiKey(apiKey);
|
|
45
50
|
}
|
|
46
|
-
/**
|
|
47
|
-
* Initialize the API key auth method.
|
|
48
|
-
* No additional setup is required for API key authentication.
|
|
49
|
-
*
|
|
50
|
-
* @param scrawn - The Scrawn SDK instance (optional, unused)
|
|
51
|
-
*/
|
|
52
|
-
async init(scrawn) {
|
|
53
|
-
// nothing extra for now
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
51
|
/**
|
|
57
52
|
* Get the API key credentials.
|
|
58
53
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"apiKeyAuth.js","sourceRoot":"","sources":["../../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AAC,YAAY,EAAE,CAAC;IACvC,CAAC;CACF"}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { Scrawn } from '../scrawn.js';
|
|
2
1
|
/**
|
|
3
2
|
* Abstract base class for authentication methods.
|
|
4
3
|
*
|
|
@@ -12,12 +11,6 @@ import type { Scrawn } from '../scrawn.js';
|
|
|
12
11
|
* type MyAuthCreds = { token: string };
|
|
13
12
|
*
|
|
14
13
|
* export class MyAuth extends AuthBase<MyAuthCreds> {
|
|
15
|
-
* name = 'my_auth';
|
|
16
|
-
*
|
|
17
|
-
* async init(scrawn: Scrawn) {
|
|
18
|
-
* // Setup logic here
|
|
19
|
-
* }
|
|
20
|
-
*
|
|
21
14
|
* async getCreds(): Promise<MyAuthCreds> {
|
|
22
15
|
* return { token: 'my_token' };
|
|
23
16
|
* }
|
|
@@ -25,21 +18,6 @@ import type { Scrawn } from '../scrawn.js';
|
|
|
25
18
|
* ```
|
|
26
19
|
*/
|
|
27
20
|
export declare abstract class AuthBase<TCreds = unknown> {
|
|
28
|
-
/**
|
|
29
|
-
* Unique identifier for this authentication method.
|
|
30
|
-
* Used to register and reference the auth method (e.g., 'api', 'oauth').
|
|
31
|
-
*/
|
|
32
|
-
abstract name: string;
|
|
33
|
-
/**
|
|
34
|
-
* Initialize the authentication method.
|
|
35
|
-
*
|
|
36
|
-
* Called once during SDK setup. Use this to perform any necessary
|
|
37
|
-
* initialization like token validation or refresh.
|
|
38
|
-
*
|
|
39
|
-
* @param scrawn - The Scrawn SDK instance
|
|
40
|
-
* @returns A promise that resolves when initialization is complete
|
|
41
|
-
*/
|
|
42
|
-
abstract init(scrawn: Scrawn): Promise<void>;
|
|
43
21
|
/**
|
|
44
22
|
* Retrieve the current credentials for this authentication method.
|
|
45
23
|
*
|
|
@@ -73,20 +51,20 @@ export declare abstract class AuthBase<TCreds = unknown> {
|
|
|
73
51
|
*/
|
|
74
52
|
preRun?(): Promise<void>;
|
|
75
53
|
/**
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
54
|
+
* Optional hook that runs after each event is processed.
|
|
55
|
+
*
|
|
56
|
+
* Use this for cleanup operations or logging that should happen
|
|
57
|
+
* after every request completes.
|
|
58
|
+
*
|
|
59
|
+
* @returns A promise that resolves when the post-run hook completes
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* async postRun() {
|
|
64
|
+
* await this.logRequestMetrics();
|
|
65
|
+
* }
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
90
68
|
postRun?(): Promise<void>;
|
|
91
69
|
}
|
|
92
70
|
//# sourceMappingURL=baseAuth.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"baseAuth.d.ts","sourceRoot":"","sources":["../../../src/core/auth/baseAuth.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"baseAuth.d.ts","sourceRoot":"","sources":["../../../src/core/auth/baseAuth.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,8BAAsB,QAAQ,CAAC,MAAM,GAAG,OAAO;IAC7C;;;;;;;;;;;;;;OAcG;IACH,QAAQ,CAAC,QAAQ,IAAI,OAAO,CAAC,MAAM,CAAC;IAEpC;;;;;;;;;;;;;;OAcG;IACH,MAAM,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC;IAExB;;;;;;;;;;;;;;OAcG;IACH,OAAO,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC;CAC1B"}
|
|
@@ -11,12 +11,6 @@
|
|
|
11
11
|
* type MyAuthCreds = { token: string };
|
|
12
12
|
*
|
|
13
13
|
* export class MyAuth extends AuthBase<MyAuthCreds> {
|
|
14
|
-
* name = 'my_auth';
|
|
15
|
-
*
|
|
16
|
-
* async init(scrawn: Scrawn) {
|
|
17
|
-
* // Setup logic here
|
|
18
|
-
* }
|
|
19
|
-
*
|
|
20
14
|
* async getCreds(): Promise<MyAuthCreds> {
|
|
21
15
|
* return { token: 'my_token' };
|
|
22
16
|
* }
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"baseAuth.js","sourceRoot":"","sources":["../../../src/core/auth/baseAuth.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"baseAuth.js","sourceRoot":"","sources":["../../../src/core/auth/baseAuth.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,OAAgB,QAAQ;CAmD7B"}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Comprehensive error handling system for the Scrawn SDK.
|
|
3
|
+
*
|
|
4
|
+
* Provides structured error classes with rich metadata for better debugging
|
|
5
|
+
* and error handling by SDK consumers.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* try {
|
|
10
|
+
* await scrawn.sdkCallEventConsumer({ ... });
|
|
11
|
+
* } catch (error) {
|
|
12
|
+
* if (error instanceof ScrawnAuthenticationError) {
|
|
13
|
+
* console.error('Auth failed:', error.message);
|
|
14
|
+
* // Refresh API key
|
|
15
|
+
* } else if (error instanceof ScrawnNetworkError && error.retryable) {
|
|
16
|
+
* // Retry the request
|
|
17
|
+
* }
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* Base error class for all Scrawn SDK errors.
|
|
23
|
+
* Extends native Error with additional metadata fields.
|
|
24
|
+
*/
|
|
25
|
+
export declare class ScrawnError extends Error {
|
|
26
|
+
/** Error code for programmatic error handling */
|
|
27
|
+
readonly code: string;
|
|
28
|
+
/** Whether this error is retryable */
|
|
29
|
+
readonly retryable: boolean;
|
|
30
|
+
/** HTTP status code if applicable */
|
|
31
|
+
readonly statusCode?: number;
|
|
32
|
+
/** Request ID for debugging (if available) */
|
|
33
|
+
readonly requestId?: string;
|
|
34
|
+
/** Additional error details */
|
|
35
|
+
readonly details?: Record<string, any>;
|
|
36
|
+
/** Original error that caused this error (if any) */
|
|
37
|
+
readonly cause?: Error;
|
|
38
|
+
constructor(message: string, options: {
|
|
39
|
+
code: string;
|
|
40
|
+
retryable?: boolean;
|
|
41
|
+
statusCode?: number;
|
|
42
|
+
requestId?: string;
|
|
43
|
+
details?: Record<string, any>;
|
|
44
|
+
cause?: Error;
|
|
45
|
+
});
|
|
46
|
+
/**
|
|
47
|
+
* Convert error to a plain object for logging/serialization.
|
|
48
|
+
*/
|
|
49
|
+
toJSON(): {
|
|
50
|
+
name: string;
|
|
51
|
+
code: string;
|
|
52
|
+
message: string;
|
|
53
|
+
retryable: boolean;
|
|
54
|
+
statusCode: number | undefined;
|
|
55
|
+
requestId: string | undefined;
|
|
56
|
+
details: Record<string, any> | undefined;
|
|
57
|
+
stack: string | undefined;
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Authentication or authorization error (401, 403).
|
|
62
|
+
* Thrown when API key is invalid, expired, or lacks permissions.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```typescript
|
|
66
|
+
* throw new ScrawnAuthenticationError('Invalid API key', {
|
|
67
|
+
* statusCode: 401,
|
|
68
|
+
* requestId: 'req_123'
|
|
69
|
+
* });
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
export declare class ScrawnAuthenticationError extends ScrawnError {
|
|
73
|
+
constructor(message: string, options?: {
|
|
74
|
+
statusCode?: number;
|
|
75
|
+
requestId?: string;
|
|
76
|
+
details?: Record<string, any>;
|
|
77
|
+
cause?: Error;
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Validation error (400).
|
|
82
|
+
* Thrown when request payload fails validation.
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```typescript
|
|
86
|
+
* throw new ScrawnValidationError('Invalid userId', {
|
|
87
|
+
* details: { field: 'userId', constraint: 'non-empty' }
|
|
88
|
+
* });
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
export declare class ScrawnValidationError extends ScrawnError {
|
|
92
|
+
constructor(message: string, options?: {
|
|
93
|
+
requestId?: string;
|
|
94
|
+
details?: Record<string, any>;
|
|
95
|
+
cause?: Error;
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Rate limit error (429).
|
|
100
|
+
* Thrown when API rate limits are exceeded.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```typescript
|
|
104
|
+
* throw new ScrawnRateLimitError('Rate limit exceeded', {
|
|
105
|
+
* details: { retryAfter: 60 }
|
|
106
|
+
* });
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
export declare class ScrawnRateLimitError extends ScrawnError {
|
|
110
|
+
/** Seconds to wait before retrying (if provided by API) */
|
|
111
|
+
readonly retryAfter?: number;
|
|
112
|
+
constructor(message: string, options?: {
|
|
113
|
+
requestId?: string;
|
|
114
|
+
retryAfter?: number;
|
|
115
|
+
details?: Record<string, any>;
|
|
116
|
+
cause?: Error;
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Network-related error (timeout, connection failure, DNS issues).
|
|
121
|
+
* These are typically retryable.
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```typescript
|
|
125
|
+
* throw new ScrawnNetworkError('Connection timeout', {
|
|
126
|
+
* cause: originalError,
|
|
127
|
+
* details: { timeout: 30000 }
|
|
128
|
+
* });
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
export declare class ScrawnNetworkError extends ScrawnError {
|
|
132
|
+
constructor(message: string, options?: {
|
|
133
|
+
requestId?: string;
|
|
134
|
+
details?: Record<string, any>;
|
|
135
|
+
cause?: Error;
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* API error from the Scrawn backend (5xx or other server errors).
|
|
140
|
+
* May be retryable depending on status code.
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```typescript
|
|
144
|
+
* throw new ScrawnAPIError('Internal server error', {
|
|
145
|
+
* statusCode: 500,
|
|
146
|
+
* retryable: true,
|
|
147
|
+
* requestId: 'req_123'
|
|
148
|
+
* });
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
151
|
+
export declare class ScrawnAPIError extends ScrawnError {
|
|
152
|
+
constructor(message: string, options: {
|
|
153
|
+
statusCode: number;
|
|
154
|
+
requestId?: string;
|
|
155
|
+
retryable?: boolean;
|
|
156
|
+
details?: Record<string, any>;
|
|
157
|
+
cause?: Error;
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Configuration error (invalid SDK initialization or settings).
|
|
162
|
+
* Not retryable - requires code changes.
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ```typescript
|
|
166
|
+
* throw new ScrawnConfigError('Invalid baseURL format', {
|
|
167
|
+
* details: { baseURL: 'invalid-url' }
|
|
168
|
+
* });
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
171
|
+
export declare class ScrawnConfigError extends ScrawnError {
|
|
172
|
+
constructor(message: string, options?: {
|
|
173
|
+
details?: Record<string, any>;
|
|
174
|
+
cause?: Error;
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Helper function to convert gRPC errors to Scrawn errors.
|
|
179
|
+
* Maps gRPC status codes to appropriate Scrawn error types.
|
|
180
|
+
*
|
|
181
|
+
* @internal
|
|
182
|
+
*/
|
|
183
|
+
export declare function convertGrpcError(error: unknown, requestId?: string): ScrawnError;
|
|
184
|
+
/**
|
|
185
|
+
* Helper to check if an error is a Scrawn error.
|
|
186
|
+
*/
|
|
187
|
+
export declare function isScrawnError(error: unknown): error is ScrawnError;
|
|
188
|
+
/**
|
|
189
|
+
* Helper to check if an error is retryable.
|
|
190
|
+
*/
|
|
191
|
+
export declare function isRetryableError(error: unknown): boolean;
|
|
192
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/core/errors/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH;;;GAGG;AACH,qBAAa,WAAY,SAAQ,KAAK;IACpC,iDAAiD;IACjD,SAAgB,IAAI,EAAE,MAAM,CAAC;IAE7B,sCAAsC;IACtC,SAAgB,SAAS,EAAE,OAAO,CAAC;IAEnC,qCAAqC;IACrC,SAAgB,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpC,8CAA8C;IAC9C,SAAgB,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnC,+BAA+B;IAC/B,SAAgB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAE9C,qDAAqD;IACrD,SAAgB,KAAK,CAAC,EAAE,KAAK,CAAC;gBAG5B,OAAO,EAAE,MAAM,EACf,OAAO,EAAE;QACP,IAAI,EAAE,MAAM,CAAC;QACb,SAAS,CAAC,EAAE,OAAO,CAAC;QACpB,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,SAAS,CAAC,EAAE,MAAM,CAAC;QACnB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;QAC9B,KAAK,CAAC,EAAE,KAAK,CAAC;KACf;IAiBH;;OAEG;IAEH,MAAM;;;;;;;;;;CAYP;AAED;;;;;;;;;;;GAWG;AACH,qBAAa,yBAA0B,SAAQ,WAAW;gBAEtD,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE;QACR,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,SAAS,CAAC,EAAE,MAAM,CAAC;QACnB,OAAO,CAA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