@predictorsdk/client 0.1.0 → 0.1.2
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 +1 -1
- package/dist/Client.js +47 -7
- package/dist/api/client/requests/GetSportsMatchingMarketsRequest.d.ts +4 -4
- package/dist/api/types/ErrorResponse.d.ts +1 -1
- package/dist/api/types/PlatformMarket.d.ts +9 -17
- package/dist/api/types/PlatformMarket.js +1 -9
- package/dist/api/types/PlatformMarketPlatform.d.ts +7 -0
- package/dist/api/types/PlatformMarketPlatform.js +7 -0
- package/dist/api/types/index.d.ts +1 -0
- package/dist/api/types/index.js +1 -0
- package/dist/core/auth/BasicAuth.d.ts +2 -2
- package/dist/core/auth/BasicAuth.js +6 -1
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.js +1 -0
- package/dist/core/schemas/Schema.d.ts +88 -0
- package/dist/core/schemas/Schema.js +22 -0
- package/dist/core/schemas/builders/bigint/bigint.d.ts +2 -0
- package/dist/core/schemas/builders/bigint/bigint.js +53 -0
- package/dist/core/schemas/builders/bigint/index.d.ts +1 -0
- package/dist/core/schemas/builders/bigint/index.js +1 -0
- package/dist/core/schemas/builders/date/date.d.ts +2 -0
- package/dist/core/schemas/builders/date/date.js +62 -0
- package/dist/core/schemas/builders/date/index.d.ts +1 -0
- package/dist/core/schemas/builders/date/index.js +1 -0
- package/dist/core/schemas/builders/enum/enum.d.ts +2 -0
- package/dist/core/schemas/builders/enum/enum.js +35 -0
- package/dist/core/schemas/builders/enum/index.d.ts +1 -0
- package/dist/core/schemas/builders/enum/index.js +1 -0
- package/dist/core/schemas/builders/index.d.ts +14 -0
- package/dist/core/schemas/builders/index.js +14 -0
- package/dist/core/schemas/builders/lazy/index.d.ts +3 -0
- package/dist/core/schemas/builders/lazy/index.js +2 -0
- package/dist/core/schemas/builders/lazy/lazy.d.ts +5 -0
- package/dist/core/schemas/builders/lazy/lazy.js +22 -0
- package/dist/core/schemas/builders/lazy/lazyObject.d.ts +3 -0
- package/dist/core/schemas/builders/lazy/lazyObject.js +17 -0
- package/dist/core/schemas/builders/list/index.d.ts +1 -0
- package/dist/core/schemas/builders/list/index.js +1 -0
- package/dist/core/schemas/builders/list/list.d.ts +2 -0
- package/dist/core/schemas/builders/list/list.js +49 -0
- package/dist/core/schemas/builders/literals/booleanLiteral.d.ts +2 -0
- package/dist/core/schemas/builders/literals/booleanLiteral.js +25 -0
- package/dist/core/schemas/builders/literals/index.d.ts +2 -0
- package/dist/core/schemas/builders/literals/index.js +2 -0
- package/dist/core/schemas/builders/literals/stringLiteral.d.ts +2 -0
- package/dist/core/schemas/builders/literals/stringLiteral.js +25 -0
- package/dist/core/schemas/builders/object/index.d.ts +6 -0
- package/dist/core/schemas/builders/object/index.js +3 -0
- package/dist/core/schemas/builders/object/object.d.ts +3 -0
- package/dist/core/schemas/builders/object/object.js +381 -0
- package/dist/core/schemas/builders/object/objectWithoutOptionalProperties.d.ts +6 -0
- package/dist/core/schemas/builders/object/objectWithoutOptionalProperties.js +4 -0
- package/dist/core/schemas/builders/object/property.d.ts +8 -0
- package/dist/core/schemas/builders/object/property.js +11 -0
- package/dist/core/schemas/builders/object/types.d.ts +31 -0
- package/dist/core/schemas/builders/object/types.js +1 -0
- package/dist/core/schemas/builders/object-like/getObjectLikeUtils.d.ts +9 -0
- package/dist/core/schemas/builders/object-like/getObjectLikeUtils.js +62 -0
- package/dist/core/schemas/builders/object-like/index.d.ts +2 -0
- package/dist/core/schemas/builders/object-like/index.js +1 -0
- package/dist/core/schemas/builders/object-like/types.d.ts +7 -0
- package/dist/core/schemas/builders/object-like/types.js +1 -0
- package/dist/core/schemas/builders/primitives/any.d.ts +2 -0
- package/dist/core/schemas/builders/primitives/any.js +6 -0
- package/dist/core/schemas/builders/primitives/boolean.d.ts +2 -0
- package/dist/core/schemas/builders/primitives/boolean.js +22 -0
- package/dist/core/schemas/builders/primitives/index.d.ts +6 -0
- package/dist/core/schemas/builders/primitives/index.js +6 -0
- package/dist/core/schemas/builders/primitives/never.d.ts +2 -0
- package/dist/core/schemas/builders/primitives/never.js +11 -0
- package/dist/core/schemas/builders/primitives/number.d.ts +2 -0
- package/dist/core/schemas/builders/primitives/number.js +22 -0
- package/dist/core/schemas/builders/primitives/string.d.ts +2 -0
- package/dist/core/schemas/builders/primitives/string.js +22 -0
- package/dist/core/schemas/builders/primitives/unknown.d.ts +2 -0
- package/dist/core/schemas/builders/primitives/unknown.js +3 -0
- package/dist/core/schemas/builders/record/index.d.ts +2 -0
- package/dist/core/schemas/builders/record/index.js +1 -0
- package/dist/core/schemas/builders/record/record.d.ts +4 -0
- package/dist/core/schemas/builders/record/record.js +133 -0
- package/dist/core/schemas/builders/record/types.d.ts +6 -0
- package/dist/core/schemas/builders/record/types.js +1 -0
- package/dist/core/schemas/builders/schema-utils/JsonError.d.ts +5 -0
- package/dist/core/schemas/builders/schema-utils/JsonError.js +9 -0
- package/dist/core/schemas/builders/schema-utils/ParseError.d.ts +5 -0
- package/dist/core/schemas/builders/schema-utils/ParseError.js +9 -0
- package/dist/core/schemas/builders/schema-utils/getSchemaUtils.d.ts +21 -0
- package/dist/core/schemas/builders/schema-utils/getSchemaUtils.js +150 -0
- package/dist/core/schemas/builders/schema-utils/index.d.ts +4 -0
- package/dist/core/schemas/builders/schema-utils/index.js +3 -0
- package/dist/core/schemas/builders/schema-utils/stringifyValidationErrors.d.ts +2 -0
- package/dist/core/schemas/builders/schema-utils/stringifyValidationErrors.js +6 -0
- package/dist/core/schemas/builders/set/index.d.ts +1 -0
- package/dist/core/schemas/builders/set/index.js +1 -0
- package/dist/core/schemas/builders/set/set.d.ts +2 -0
- package/dist/core/schemas/builders/set/set.js +42 -0
- package/dist/core/schemas/builders/undiscriminated-union/index.d.ts +2 -0
- package/dist/core/schemas/builders/undiscriminated-union/index.js +1 -0
- package/dist/core/schemas/builders/undiscriminated-union/types.d.ts +4 -0
- package/dist/core/schemas/builders/undiscriminated-union/types.js +1 -0
- package/dist/core/schemas/builders/undiscriminated-union/undiscriminatedUnion.d.ts +3 -0
- package/dist/core/schemas/builders/undiscriminated-union/undiscriminatedUnion.js +39 -0
- package/dist/core/schemas/builders/union/discriminant.d.ts +5 -0
- package/dist/core/schemas/builders/union/discriminant.js +6 -0
- package/dist/core/schemas/builders/union/index.d.ts +4 -0
- package/dist/core/schemas/builders/union/index.js +2 -0
- package/dist/core/schemas/builders/union/types.d.ts +13 -0
- package/dist/core/schemas/builders/union/types.js +1 -0
- package/dist/core/schemas/builders/union/union.d.ts +4 -0
- package/dist/core/schemas/builders/union/union.js +127 -0
- package/dist/core/schemas/index.d.ts +2 -0
- package/dist/core/schemas/index.js +1 -0
- package/dist/core/schemas/utils/MaybePromise.d.ts +1 -0
- package/dist/core/schemas/utils/MaybePromise.js +1 -0
- package/dist/core/schemas/utils/addQuestionMarksToNullableProperties.d.ts +7 -0
- package/dist/core/schemas/utils/addQuestionMarksToNullableProperties.js +1 -0
- package/dist/core/schemas/utils/createIdentitySchemaCreator.d.ts +2 -0
- package/dist/core/schemas/utils/createIdentitySchemaCreator.js +15 -0
- package/dist/core/schemas/utils/entries.d.ts +1 -0
- package/dist/core/schemas/utils/entries.js +3 -0
- package/dist/core/schemas/utils/filterObject.d.ts +1 -0
- package/dist/core/schemas/utils/filterObject.js +10 -0
- package/dist/core/schemas/utils/getErrorMessageForIncorrectType.d.ts +1 -0
- package/dist/core/schemas/utils/getErrorMessageForIncorrectType.js +24 -0
- package/dist/core/schemas/utils/isPlainObject.d.ts +1 -0
- package/dist/core/schemas/utils/isPlainObject.js +12 -0
- package/dist/core/schemas/utils/keys.d.ts +1 -0
- package/dist/core/schemas/utils/keys.js +3 -0
- package/dist/core/schemas/utils/maybeSkipValidation.d.ts +2 -0
- package/dist/core/schemas/utils/maybeSkipValidation.js +28 -0
- package/dist/core/schemas/utils/partition.d.ts +1 -0
- package/dist/core/schemas/utils/partition.js +12 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/serialization/index.d.ts +1 -0
- package/dist/serialization/index.js +1 -0
- package/dist/serialization/types/ErrorResponse.d.ts +10 -0
- package/dist/serialization/types/ErrorResponse.js +6 -0
- package/dist/serialization/types/PlatformMarket.d.ts +16 -0
- package/dist/serialization/types/PlatformMarket.js +12 -0
- package/dist/serialization/types/PlatformMarketPlatform.d.ts +7 -0
- package/dist/serialization/types/PlatformMarketPlatform.js +3 -0
- package/dist/serialization/types/SportsMatchingResponse.d.ts +10 -0
- package/dist/serialization/types/SportsMatchingResponse.js +6 -0
- package/dist/serialization/types/index.d.ts +4 -0
- package/dist/serialization/types/index.js +4 -0
- package/package.json +13 -2
- package/LICENSE +0 -21
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@ import { PredictorSDKClient } from "@predictorsdk/client";
|
|
|
16
16
|
const client = new PredictorSDKClient({ token: "your-api-key" });
|
|
17
17
|
|
|
18
18
|
const { markets } = await client.getSportsMatchingMarkets({
|
|
19
|
-
|
|
19
|
+
kalshiEventTicker: "KXMLB-25-NYM-COL-2025-04-03",
|
|
20
20
|
});
|
|
21
21
|
```
|
|
22
22
|
|
package/dist/Client.js
CHANGED
|
@@ -6,6 +6,7 @@ import * as core from "./core/index.js";
|
|
|
6
6
|
import * as environments from "./environments.js";
|
|
7
7
|
import { handleNonStatusCodeError } from "./errors/handleNonStatusCodeError.js";
|
|
8
8
|
import * as errors from "./errors/index.js";
|
|
9
|
+
import * as serializers from "./serialization/index.js";
|
|
9
10
|
export class PredictorSDKClient {
|
|
10
11
|
_options;
|
|
11
12
|
constructor(options) {
|
|
@@ -30,7 +31,7 @@ export class PredictorSDKClient {
|
|
|
30
31
|
return core.HttpResponsePromise.fromPromise(this.__getSportsMatchingMarkets(request, requestOptions));
|
|
31
32
|
}
|
|
32
33
|
async __getSportsMatchingMarkets(request = {}, requestOptions) {
|
|
33
|
-
const {
|
|
34
|
+
const { kalshiEventTicker, polymarketMarketSlug, predictMarketId, sxbetMarketId } = request;
|
|
34
35
|
const _queryParams = {
|
|
35
36
|
kalshi_event_ticker: kalshiEventTicker,
|
|
36
37
|
polymarket_market_slug: polymarketMarketSlug,
|
|
@@ -53,20 +54,59 @@ export class PredictorSDKClient {
|
|
|
53
54
|
logging: this._options.logging,
|
|
54
55
|
});
|
|
55
56
|
if (_response.ok) {
|
|
56
|
-
return {
|
|
57
|
+
return {
|
|
58
|
+
data: serializers.SportsMatchingResponse.parseOrThrow(_response.body, {
|
|
59
|
+
unrecognizedObjectKeys: "passthrough",
|
|
60
|
+
allowUnrecognizedUnionMembers: true,
|
|
61
|
+
allowUnrecognizedEnumValues: true,
|
|
62
|
+
skipValidation: true,
|
|
63
|
+
breadcrumbsPrefix: ["response"],
|
|
64
|
+
}),
|
|
65
|
+
rawResponse: _response.rawResponse,
|
|
66
|
+
};
|
|
57
67
|
}
|
|
58
68
|
if (_response.error.reason === "status-code") {
|
|
59
69
|
switch (_response.error.statusCode) {
|
|
60
70
|
case 400:
|
|
61
|
-
throw new PredictorSDK.BadRequestError(_response.error.body,
|
|
71
|
+
throw new PredictorSDK.BadRequestError(serializers.ErrorResponse.parseOrThrow(_response.error.body, {
|
|
72
|
+
unrecognizedObjectKeys: "passthrough",
|
|
73
|
+
allowUnrecognizedUnionMembers: true,
|
|
74
|
+
allowUnrecognizedEnumValues: true,
|
|
75
|
+
skipValidation: true,
|
|
76
|
+
breadcrumbsPrefix: ["response"],
|
|
77
|
+
}), _response.rawResponse);
|
|
62
78
|
case 401:
|
|
63
|
-
throw new PredictorSDK.UnauthorizedError(_response.error.body,
|
|
79
|
+
throw new PredictorSDK.UnauthorizedError(serializers.ErrorResponse.parseOrThrow(_response.error.body, {
|
|
80
|
+
unrecognizedObjectKeys: "passthrough",
|
|
81
|
+
allowUnrecognizedUnionMembers: true,
|
|
82
|
+
allowUnrecognizedEnumValues: true,
|
|
83
|
+
skipValidation: true,
|
|
84
|
+
breadcrumbsPrefix: ["response"],
|
|
85
|
+
}), _response.rawResponse);
|
|
64
86
|
case 403:
|
|
65
|
-
throw new PredictorSDK.ForbiddenError(_response.error.body,
|
|
87
|
+
throw new PredictorSDK.ForbiddenError(serializers.ErrorResponse.parseOrThrow(_response.error.body, {
|
|
88
|
+
unrecognizedObjectKeys: "passthrough",
|
|
89
|
+
allowUnrecognizedUnionMembers: true,
|
|
90
|
+
allowUnrecognizedEnumValues: true,
|
|
91
|
+
skipValidation: true,
|
|
92
|
+
breadcrumbsPrefix: ["response"],
|
|
93
|
+
}), _response.rawResponse);
|
|
66
94
|
case 429:
|
|
67
|
-
throw new PredictorSDK.TooManyRequestsError(_response.error.body,
|
|
95
|
+
throw new PredictorSDK.TooManyRequestsError(serializers.ErrorResponse.parseOrThrow(_response.error.body, {
|
|
96
|
+
unrecognizedObjectKeys: "passthrough",
|
|
97
|
+
allowUnrecognizedUnionMembers: true,
|
|
98
|
+
allowUnrecognizedEnumValues: true,
|
|
99
|
+
skipValidation: true,
|
|
100
|
+
breadcrumbsPrefix: ["response"],
|
|
101
|
+
}), _response.rawResponse);
|
|
68
102
|
case 503:
|
|
69
|
-
throw new PredictorSDK.ServiceUnavailableError(_response.error.body,
|
|
103
|
+
throw new PredictorSDK.ServiceUnavailableError(serializers.ErrorResponse.parseOrThrow(_response.error.body, {
|
|
104
|
+
unrecognizedObjectKeys: "passthrough",
|
|
105
|
+
allowUnrecognizedUnionMembers: true,
|
|
106
|
+
allowUnrecognizedEnumValues: true,
|
|
107
|
+
skipValidation: true,
|
|
108
|
+
breadcrumbsPrefix: ["response"],
|
|
109
|
+
}), _response.rawResponse);
|
|
70
110
|
default:
|
|
71
111
|
throw new errors.PredictorSDKError({
|
|
72
112
|
statusCode: _response.error.statusCode,
|
|
@@ -4,11 +4,11 @@
|
|
|
4
4
|
*/
|
|
5
5
|
export interface GetSportsMatchingMarketsRequest {
|
|
6
6
|
/** Kalshi event ticker(s) to find matching markets for (e.g. `KXNFLGAME-25AUG16ARIDEN`). Provide the parameter multiple times for multiple tickers. Only one filter type may be used per request. */
|
|
7
|
-
|
|
7
|
+
kalshiEventTicker?: string | string[];
|
|
8
8
|
/** Polymarket market slug(s) to find matching markets for (e.g. `nfl-ari-den-2025-08-16`). Provide the parameter multiple times for multiple slugs. Only one filter type may be used per request. */
|
|
9
|
-
|
|
9
|
+
polymarketMarketSlug?: string | string[];
|
|
10
10
|
/** Predict market ID(s) to find matching markets for (e.g. `110629`). Provide the parameter multiple times for multiple IDs. Only one filter type may be used per request. */
|
|
11
|
-
|
|
11
|
+
predictMarketId?: string | string[];
|
|
12
12
|
/** SX Bet market ID(s) to find matching markets for (e.g. `0x4c000abdbf197ef32ecdf15561b1d636f1e5b02629f466678757fd83e2ec3599`). Provide the parameter multiple times for multiple IDs. Only one filter type may be used per request. */
|
|
13
|
-
|
|
13
|
+
sxbetMarketId?: string | string[];
|
|
14
14
|
}
|
|
@@ -1,24 +1,16 @@
|
|
|
1
|
+
import type * as PredictorSDK from "../index.js";
|
|
1
2
|
export interface PlatformMarket {
|
|
2
|
-
platform:
|
|
3
|
+
platform: PredictorSDK.PlatformMarketPlatform;
|
|
3
4
|
/** Kalshi event ticker. Present when platform is KALSHI. */
|
|
4
|
-
|
|
5
|
+
eventTicker?: string;
|
|
5
6
|
/** Kalshi market tickers. Present when platform is KALSHI. */
|
|
6
|
-
|
|
7
|
+
marketTickers?: string[];
|
|
7
8
|
/** Polymarket market slug. Present when platform is POLYMARKET. */
|
|
8
|
-
|
|
9
|
+
marketSlug?: string;
|
|
9
10
|
/** Polymarket token IDs. Present when platform is POLYMARKET. */
|
|
10
|
-
|
|
11
|
+
tokenIds?: string[];
|
|
11
12
|
/** Source market ID. Present for platforms other than Kalshi and Polymarket. */
|
|
12
|
-
|
|
13
|
-
/** Source outcome IDs. Present for platforms
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
export declare namespace PlatformMarket {
|
|
17
|
-
const Platform: {
|
|
18
|
-
readonly Kalshi: "KALSHI";
|
|
19
|
-
readonly Polymarket: "POLYMARKET";
|
|
20
|
-
readonly Predict: "PREDICT";
|
|
21
|
-
readonly Sxbet: "SXBET";
|
|
22
|
-
};
|
|
23
|
-
type Platform = (typeof Platform)[keyof typeof Platform];
|
|
13
|
+
marketId?: string;
|
|
14
|
+
/** Source outcome IDs. Present for platforms that use outcome IDs. */
|
|
15
|
+
outcomeIds?: string[];
|
|
24
16
|
}
|
|
@@ -1,10 +1,2 @@
|
|
|
1
1
|
// This file was auto-generated by Fern from our API Definition.
|
|
2
|
-
export
|
|
3
|
-
(function (PlatformMarket) {
|
|
4
|
-
PlatformMarket.Platform = {
|
|
5
|
-
Kalshi: "KALSHI",
|
|
6
|
-
Polymarket: "POLYMARKET",
|
|
7
|
-
Predict: "PREDICT",
|
|
8
|
-
Sxbet: "SXBET",
|
|
9
|
-
};
|
|
10
|
-
})(PlatformMarket || (PlatformMarket = {}));
|
|
2
|
+
export {};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const PlatformMarketPlatform: {
|
|
2
|
+
readonly Kalshi: "KALSHI";
|
|
3
|
+
readonly Polymarket: "POLYMARKET";
|
|
4
|
+
readonly Predict: "PREDICT";
|
|
5
|
+
readonly Sxbet: "SXBET";
|
|
6
|
+
};
|
|
7
|
+
export type PlatformMarketPlatform = (typeof PlatformMarketPlatform)[keyof typeof PlatformMarketPlatform];
|
package/dist/api/types/index.js
CHANGED
|
@@ -5,7 +5,12 @@ export const BasicAuth = {
|
|
|
5
5
|
if (basicAuth == null) {
|
|
6
6
|
return undefined;
|
|
7
7
|
}
|
|
8
|
-
const
|
|
8
|
+
const username = basicAuth.username ?? "";
|
|
9
|
+
const password = basicAuth.password ?? "";
|
|
10
|
+
if (username === "" && password === "") {
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
13
|
+
const token = base64Encode(`${username}:${password}`);
|
|
9
14
|
return `Basic ${token}`;
|
|
10
15
|
},
|
|
11
16
|
fromAuthorizationHeader: (header) => {
|
package/dist/core/index.d.ts
CHANGED
package/dist/core/index.js
CHANGED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { SchemaUtils } from "./builders/index.js";
|
|
2
|
+
export type Schema<Raw = unknown, Parsed = unknown> = BaseSchema<Raw, Parsed> & SchemaUtils<Raw, Parsed>;
|
|
3
|
+
export type inferRaw<S extends Schema> = S extends Schema<infer Raw, any> ? Raw : never;
|
|
4
|
+
export type inferParsed<S extends Schema> = S extends Schema<any, infer Parsed> ? Parsed : never;
|
|
5
|
+
export interface BaseSchema<Raw, Parsed> {
|
|
6
|
+
parse: (raw: unknown, opts?: SchemaOptions) => MaybeValid<Parsed>;
|
|
7
|
+
json: (parsed: unknown, opts?: SchemaOptions) => MaybeValid<Raw>;
|
|
8
|
+
getType: () => SchemaType | SchemaType;
|
|
9
|
+
}
|
|
10
|
+
export declare const SchemaType: {
|
|
11
|
+
readonly BIGINT: "bigint";
|
|
12
|
+
readonly DATE: "date";
|
|
13
|
+
readonly ENUM: "enum";
|
|
14
|
+
readonly LIST: "list";
|
|
15
|
+
readonly STRING_LITERAL: "stringLiteral";
|
|
16
|
+
readonly BOOLEAN_LITERAL: "booleanLiteral";
|
|
17
|
+
readonly OBJECT: "object";
|
|
18
|
+
readonly ANY: "any";
|
|
19
|
+
readonly BOOLEAN: "boolean";
|
|
20
|
+
readonly NUMBER: "number";
|
|
21
|
+
readonly STRING: "string";
|
|
22
|
+
readonly UNKNOWN: "unknown";
|
|
23
|
+
readonly NEVER: "never";
|
|
24
|
+
readonly RECORD: "record";
|
|
25
|
+
readonly SET: "set";
|
|
26
|
+
readonly UNION: "union";
|
|
27
|
+
readonly UNDISCRIMINATED_UNION: "undiscriminatedUnion";
|
|
28
|
+
readonly NULLABLE: "nullable";
|
|
29
|
+
readonly OPTIONAL: "optional";
|
|
30
|
+
readonly OPTIONAL_NULLABLE: "optionalNullable";
|
|
31
|
+
};
|
|
32
|
+
export type SchemaType = (typeof SchemaType)[keyof typeof SchemaType];
|
|
33
|
+
export type MaybeValid<T> = Valid<T> | Invalid;
|
|
34
|
+
export interface Valid<T> {
|
|
35
|
+
ok: true;
|
|
36
|
+
value: T;
|
|
37
|
+
}
|
|
38
|
+
export interface Invalid {
|
|
39
|
+
ok: false;
|
|
40
|
+
errors: ValidationError[];
|
|
41
|
+
}
|
|
42
|
+
export interface ValidationError {
|
|
43
|
+
path: string[];
|
|
44
|
+
message: string;
|
|
45
|
+
}
|
|
46
|
+
export interface SchemaOptions {
|
|
47
|
+
/**
|
|
48
|
+
* how to handle unrecognized keys in objects
|
|
49
|
+
*
|
|
50
|
+
* @default "fail"
|
|
51
|
+
*/
|
|
52
|
+
unrecognizedObjectKeys?: "fail" | "passthrough" | "strip";
|
|
53
|
+
/**
|
|
54
|
+
* whether to fail when an unrecognized discriminant value is
|
|
55
|
+
* encountered in a union
|
|
56
|
+
*
|
|
57
|
+
* @default false
|
|
58
|
+
*/
|
|
59
|
+
allowUnrecognizedUnionMembers?: boolean;
|
|
60
|
+
/**
|
|
61
|
+
* whether to fail when an unrecognized enum value is encountered
|
|
62
|
+
*
|
|
63
|
+
* @default false
|
|
64
|
+
*/
|
|
65
|
+
allowUnrecognizedEnumValues?: boolean;
|
|
66
|
+
/**
|
|
67
|
+
* whether to allow data that doesn't conform to the schema.
|
|
68
|
+
* invalid data is passed through without transformation.
|
|
69
|
+
*
|
|
70
|
+
* when this is enabled, .parse() and .json() will always
|
|
71
|
+
* return `ok: true`. `.parseOrThrow()` and `.jsonOrThrow()`
|
|
72
|
+
* will never fail.
|
|
73
|
+
*
|
|
74
|
+
* @default false
|
|
75
|
+
*/
|
|
76
|
+
skipValidation?: boolean;
|
|
77
|
+
/**
|
|
78
|
+
* each validation failure contains a "path" property, which is
|
|
79
|
+
* the breadcrumbs to the offending node in the JSON. you can supply
|
|
80
|
+
* a prefix that is prepended to all the errors' paths. this can be
|
|
81
|
+
* helpful for zurg's internal debug logging.
|
|
82
|
+
*/
|
|
83
|
+
breadcrumbsPrefix?: string[];
|
|
84
|
+
/**
|
|
85
|
+
* whether to send 'null' for optional properties explicitly set to 'undefined'.
|
|
86
|
+
*/
|
|
87
|
+
omitUndefined?: boolean;
|
|
88
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export const SchemaType = {
|
|
2
|
+
BIGINT: "bigint",
|
|
3
|
+
DATE: "date",
|
|
4
|
+
ENUM: "enum",
|
|
5
|
+
LIST: "list",
|
|
6
|
+
STRING_LITERAL: "stringLiteral",
|
|
7
|
+
BOOLEAN_LITERAL: "booleanLiteral",
|
|
8
|
+
OBJECT: "object",
|
|
9
|
+
ANY: "any",
|
|
10
|
+
BOOLEAN: "boolean",
|
|
11
|
+
NUMBER: "number",
|
|
12
|
+
STRING: "string",
|
|
13
|
+
UNKNOWN: "unknown",
|
|
14
|
+
NEVER: "never",
|
|
15
|
+
RECORD: "record",
|
|
16
|
+
SET: "set",
|
|
17
|
+
UNION: "union",
|
|
18
|
+
UNDISCRIMINATED_UNION: "undiscriminatedUnion",
|
|
19
|
+
NULLABLE: "nullable",
|
|
20
|
+
OPTIONAL: "optional",
|
|
21
|
+
OPTIONAL_NULLABLE: "optionalNullable",
|
|
22
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { SchemaType } from "../../Schema.js";
|
|
2
|
+
import { getErrorMessageForIncorrectType } from "../../utils/getErrorMessageForIncorrectType.js";
|
|
3
|
+
import { maybeSkipValidation } from "../../utils/maybeSkipValidation.js";
|
|
4
|
+
import { getSchemaUtils } from "../schema-utils/index.js";
|
|
5
|
+
export function bigint() {
|
|
6
|
+
const baseSchema = {
|
|
7
|
+
parse: (raw, { breadcrumbsPrefix = [] } = {}) => {
|
|
8
|
+
if (typeof raw === "bigint") {
|
|
9
|
+
return {
|
|
10
|
+
ok: true,
|
|
11
|
+
value: raw,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
if (typeof raw === "number") {
|
|
15
|
+
return {
|
|
16
|
+
ok: true,
|
|
17
|
+
value: BigInt(raw),
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
return {
|
|
21
|
+
ok: false,
|
|
22
|
+
errors: [
|
|
23
|
+
{
|
|
24
|
+
path: breadcrumbsPrefix,
|
|
25
|
+
message: getErrorMessageForIncorrectType(raw, "bigint | number"),
|
|
26
|
+
},
|
|
27
|
+
],
|
|
28
|
+
};
|
|
29
|
+
},
|
|
30
|
+
json: (bigint, { breadcrumbsPrefix = [] } = {}) => {
|
|
31
|
+
if (typeof bigint !== "bigint") {
|
|
32
|
+
return {
|
|
33
|
+
ok: false,
|
|
34
|
+
errors: [
|
|
35
|
+
{
|
|
36
|
+
path: breadcrumbsPrefix,
|
|
37
|
+
message: getErrorMessageForIncorrectType(bigint, "bigint"),
|
|
38
|
+
},
|
|
39
|
+
],
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
return {
|
|
43
|
+
ok: true,
|
|
44
|
+
value: bigint,
|
|
45
|
+
};
|
|
46
|
+
},
|
|
47
|
+
getType: () => SchemaType.BIGINT,
|
|
48
|
+
};
|
|
49
|
+
return {
|
|
50
|
+
...maybeSkipValidation(baseSchema),
|
|
51
|
+
...getSchemaUtils(baseSchema),
|
|
52
|
+
};
|
|
53
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { bigint } from "./bigint.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { bigint } from "./bigint.js";
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { SchemaType } from "../../Schema.js";
|
|
2
|
+
import { getErrorMessageForIncorrectType } from "../../utils/getErrorMessageForIncorrectType.js";
|
|
3
|
+
import { maybeSkipValidation } from "../../utils/maybeSkipValidation.js";
|
|
4
|
+
import { getSchemaUtils } from "../schema-utils/index.js";
|
|
5
|
+
// https://stackoverflow.com/questions/12756159/regex-and-iso8601-formatted-datetime
|
|
6
|
+
const ISO_8601_REGEX = /^([+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([.,]\d+(?!:))?)?(\17[0-5]\d([.,]\d+)?)?([zZ]|([+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/;
|
|
7
|
+
export function date() {
|
|
8
|
+
const baseSchema = {
|
|
9
|
+
parse: (raw, { breadcrumbsPrefix = [] } = {}) => {
|
|
10
|
+
if (typeof raw !== "string") {
|
|
11
|
+
return {
|
|
12
|
+
ok: false,
|
|
13
|
+
errors: [
|
|
14
|
+
{
|
|
15
|
+
path: breadcrumbsPrefix,
|
|
16
|
+
message: getErrorMessageForIncorrectType(raw, "string"),
|
|
17
|
+
},
|
|
18
|
+
],
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
if (!ISO_8601_REGEX.test(raw)) {
|
|
22
|
+
return {
|
|
23
|
+
ok: false,
|
|
24
|
+
errors: [
|
|
25
|
+
{
|
|
26
|
+
path: breadcrumbsPrefix,
|
|
27
|
+
message: getErrorMessageForIncorrectType(raw, "ISO 8601 date string"),
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
ok: true,
|
|
34
|
+
value: new Date(raw),
|
|
35
|
+
};
|
|
36
|
+
},
|
|
37
|
+
json: (date, { breadcrumbsPrefix = [] } = {}) => {
|
|
38
|
+
if (date instanceof Date) {
|
|
39
|
+
return {
|
|
40
|
+
ok: true,
|
|
41
|
+
value: date.toISOString(),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
return {
|
|
46
|
+
ok: false,
|
|
47
|
+
errors: [
|
|
48
|
+
{
|
|
49
|
+
path: breadcrumbsPrefix,
|
|
50
|
+
message: getErrorMessageForIncorrectType(date, "Date object"),
|
|
51
|
+
},
|
|
52
|
+
],
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
getType: () => SchemaType.DATE,
|
|
57
|
+
};
|
|
58
|
+
return {
|
|
59
|
+
...maybeSkipValidation(baseSchema),
|
|
60
|
+
...getSchemaUtils(baseSchema),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { date } from "./date.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { date } from "./date.js";
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { SchemaType } from "../../Schema.js";
|
|
2
|
+
import { createIdentitySchemaCreator } from "../../utils/createIdentitySchemaCreator.js";
|
|
3
|
+
import { getErrorMessageForIncorrectType } from "../../utils/getErrorMessageForIncorrectType.js";
|
|
4
|
+
export function enum_(values) {
|
|
5
|
+
const validValues = new Set(values);
|
|
6
|
+
const schemaCreator = createIdentitySchemaCreator(SchemaType.ENUM, (value, { allowUnrecognizedEnumValues, breadcrumbsPrefix = [] } = {}) => {
|
|
7
|
+
if (typeof value !== "string") {
|
|
8
|
+
return {
|
|
9
|
+
ok: false,
|
|
10
|
+
errors: [
|
|
11
|
+
{
|
|
12
|
+
path: breadcrumbsPrefix,
|
|
13
|
+
message: getErrorMessageForIncorrectType(value, "string"),
|
|
14
|
+
},
|
|
15
|
+
],
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
if (!validValues.has(value) && !allowUnrecognizedEnumValues) {
|
|
19
|
+
return {
|
|
20
|
+
ok: false,
|
|
21
|
+
errors: [
|
|
22
|
+
{
|
|
23
|
+
path: breadcrumbsPrefix,
|
|
24
|
+
message: getErrorMessageForIncorrectType(value, "enum"),
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
ok: true,
|
|
31
|
+
value: value,
|
|
32
|
+
};
|
|
33
|
+
});
|
|
34
|
+
return schemaCreator();
|
|
35
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { enum_ } from "./enum.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { enum_ } from "./enum.js";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export * from "./bigint/index.js";
|
|
2
|
+
export * from "./date/index.js";
|
|
3
|
+
export * from "./enum/index.js";
|
|
4
|
+
export * from "./lazy/index.js";
|
|
5
|
+
export * from "./list/index.js";
|
|
6
|
+
export * from "./literals/index.js";
|
|
7
|
+
export * from "./object/index.js";
|
|
8
|
+
export * from "./object-like/index.js";
|
|
9
|
+
export * from "./primitives/index.js";
|
|
10
|
+
export * from "./record/index.js";
|
|
11
|
+
export * from "./schema-utils/index.js";
|
|
12
|
+
export * from "./set/index.js";
|
|
13
|
+
export * from "./undiscriminated-union/index.js";
|
|
14
|
+
export * from "./union/index.js";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export * from "./bigint/index.js";
|
|
2
|
+
export * from "./date/index.js";
|
|
3
|
+
export * from "./enum/index.js";
|
|
4
|
+
export * from "./lazy/index.js";
|
|
5
|
+
export * from "./list/index.js";
|
|
6
|
+
export * from "./literals/index.js";
|
|
7
|
+
export * from "./object/index.js";
|
|
8
|
+
export * from "./object-like/index.js";
|
|
9
|
+
export * from "./primitives/index.js";
|
|
10
|
+
export * from "./record/index.js";
|
|
11
|
+
export * from "./schema-utils/index.js";
|
|
12
|
+
export * from "./set/index.js";
|
|
13
|
+
export * from "./undiscriminated-union/index.js";
|
|
14
|
+
export * from "./union/index.js";
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { BaseSchema, Schema } from "../../Schema.js";
|
|
2
|
+
export type SchemaGetter<SchemaType extends Schema<any, any>> = () => SchemaType;
|
|
3
|
+
export declare function lazy<Raw, Parsed>(getter: SchemaGetter<Schema<Raw, Parsed>>): Schema<Raw, Parsed>;
|
|
4
|
+
export declare function constructLazyBaseSchema<Raw, Parsed>(getter: SchemaGetter<Schema<Raw, Parsed>>): BaseSchema<Raw, Parsed>;
|
|
5
|
+
export declare function getMemoizedSchema<SchemaType extends Schema<any, any>>(getter: SchemaGetter<SchemaType>): SchemaType;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { getSchemaUtils } from "../schema-utils/index.js";
|
|
2
|
+
export function lazy(getter) {
|
|
3
|
+
const baseSchema = constructLazyBaseSchema(getter);
|
|
4
|
+
return {
|
|
5
|
+
...baseSchema,
|
|
6
|
+
...getSchemaUtils(baseSchema),
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
export function constructLazyBaseSchema(getter) {
|
|
10
|
+
return {
|
|
11
|
+
parse: (raw, opts) => getMemoizedSchema(getter).parse(raw, opts),
|
|
12
|
+
json: (parsed, opts) => getMemoizedSchema(getter).json(parsed, opts),
|
|
13
|
+
getType: () => getMemoizedSchema(getter).getType(),
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export function getMemoizedSchema(getter) {
|
|
17
|
+
const castedGetter = getter;
|
|
18
|
+
if (castedGetter.__zurg_memoized == null) {
|
|
19
|
+
castedGetter.__zurg_memoized = getter();
|
|
20
|
+
}
|
|
21
|
+
return castedGetter.__zurg_memoized;
|
|
22
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { getObjectUtils } from "../object/index.js";
|
|
2
|
+
import { getObjectLikeUtils } from "../object-like/index.js";
|
|
3
|
+
import { getSchemaUtils } from "../schema-utils/index.js";
|
|
4
|
+
import { constructLazyBaseSchema, getMemoizedSchema } from "./lazy.js";
|
|
5
|
+
export function lazyObject(getter) {
|
|
6
|
+
const baseSchema = {
|
|
7
|
+
...constructLazyBaseSchema(getter),
|
|
8
|
+
_getRawProperties: () => getMemoizedSchema(getter)._getRawProperties(),
|
|
9
|
+
_getParsedProperties: () => getMemoizedSchema(getter)._getParsedProperties(),
|
|
10
|
+
};
|
|
11
|
+
return {
|
|
12
|
+
...baseSchema,
|
|
13
|
+
...getSchemaUtils(baseSchema),
|
|
14
|
+
...getObjectLikeUtils(baseSchema),
|
|
15
|
+
...getObjectUtils(baseSchema),
|
|
16
|
+
};
|
|
17
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { list } from "./list.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { list } from "./list.js";
|