effect 4.0.0-beta.51 → 4.0.0-beta.53
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/dist/Data.js +2 -2
- package/dist/Data.js.map +1 -1
- package/dist/Effect.d.ts +2 -1
- package/dist/Effect.d.ts.map +1 -1
- package/dist/Effect.js.map +1 -1
- package/dist/Effectable.d.ts +29 -0
- package/dist/Effectable.d.ts.map +1 -0
- package/dist/Effectable.js +31 -0
- package/dist/Effectable.js.map +1 -0
- package/dist/Schema.d.ts +0 -1
- package/dist/Schema.d.ts.map +1 -1
- package/dist/Schema.js +83 -12
- package/dist/Schema.js.map +1 -1
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -1
- package/dist/internal/effect.js +1 -1
- package/dist/internal/effect.js.map +1 -1
- package/dist/internal/schema/representation.js +1 -2
- package/dist/internal/schema/representation.js.map +1 -1
- package/dist/internal/schema/schema.d.ts.map +1 -1
- package/dist/internal/schema/schema.js +37 -0
- package/dist/internal/schema/schema.js.map +1 -1
- package/dist/testing/TestClock.js +1 -1
- package/dist/testing/TestClock.js.map +1 -1
- package/dist/unstable/ai/McpServer.d.ts.map +1 -1
- package/dist/unstable/ai/McpServer.js +7 -0
- package/dist/unstable/ai/McpServer.js.map +1 -1
- package/dist/unstable/cli/index.d.ts +2 -0
- package/dist/unstable/cli/index.d.ts.map +1 -1
- package/dist/unstable/cli/index.js +2 -0
- package/dist/unstable/cli/index.js.map +1 -1
- package/dist/unstable/cluster/ClusterWorkflowEngine.js +1 -1
- package/dist/unstable/cluster/ClusterWorkflowEngine.js.map +1 -1
- package/dist/unstable/http/HttpIncomingMessage.d.ts.map +1 -1
- package/dist/unstable/http/HttpIncomingMessage.js +4 -8
- package/dist/unstable/http/HttpIncomingMessage.js.map +1 -1
- package/dist/unstable/httpapi/HttpApi.js.map +1 -1
- package/dist/unstable/httpapi/HttpApiBuilder.d.ts.map +1 -1
- package/dist/unstable/httpapi/HttpApiBuilder.js +11 -6
- package/dist/unstable/httpapi/HttpApiBuilder.js.map +1 -1
- package/dist/unstable/httpapi/HttpApiClient.d.ts +5 -6
- package/dist/unstable/httpapi/HttpApiClient.d.ts.map +1 -1
- package/dist/unstable/httpapi/HttpApiClient.js.map +1 -1
- package/dist/unstable/httpapi/HttpApiEndpoint.d.ts +17 -18
- package/dist/unstable/httpapi/HttpApiEndpoint.d.ts.map +1 -1
- package/dist/unstable/httpapi/HttpApiEndpoint.js +1 -2
- package/dist/unstable/httpapi/HttpApiEndpoint.js.map +1 -1
- package/dist/unstable/httpapi/HttpApiError.d.ts +28 -8
- package/dist/unstable/httpapi/HttpApiError.d.ts.map +1 -1
- package/dist/unstable/httpapi/HttpApiError.js +28 -15
- package/dist/unstable/httpapi/HttpApiError.js.map +1 -1
- package/dist/unstable/httpapi/HttpApiMiddleware.d.ts +4 -3
- package/dist/unstable/httpapi/HttpApiMiddleware.d.ts.map +1 -1
- package/dist/unstable/httpapi/HttpApiMiddleware.js +2 -2
- package/dist/unstable/httpapi/HttpApiMiddleware.js.map +1 -1
- package/dist/unstable/httpapi/OpenApi.js.map +1 -1
- package/dist/unstable/reactivity/AtomHttpApi.d.ts +7 -4
- package/dist/unstable/reactivity/AtomHttpApi.d.ts.map +1 -1
- package/dist/unstable/reactivity/AtomHttpApi.js.map +1 -1
- package/dist/unstable/sql/Statement.d.ts +1 -3
- package/dist/unstable/sql/Statement.d.ts.map +1 -1
- package/dist/unstable/sql/Statement.js +12 -11
- package/dist/unstable/sql/Statement.js.map +1 -1
- package/dist/unstable/workflow/Activity.d.ts +1 -1
- package/dist/unstable/workflow/Activity.d.ts.map +1 -1
- package/dist/unstable/workflow/Activity.js +8 -7
- package/dist/unstable/workflow/Activity.js.map +1 -1
- package/package.json +1 -1
- package/src/Data.ts +1 -1
- package/src/Effect.ts +4 -1
- package/src/Effectable.ts +47 -0
- package/src/Schema.ts +97 -13
- package/src/index.ts +8 -0
- package/src/internal/effect.ts +1 -1
- package/src/internal/schema/representation.ts +1 -2
- package/src/internal/schema/schema.ts +43 -0
- package/src/testing/TestClock.ts +1 -1
- package/src/unstable/ai/McpServer.ts +8 -0
- package/src/unstable/cli/index.ts +2 -0
- package/src/unstable/cluster/ClusterWorkflowEngine.ts +1 -1
- package/src/unstable/http/HttpIncomingMessage.ts +4 -4
- package/src/unstable/httpapi/HttpApi.ts +1 -1
- package/src/unstable/httpapi/HttpApiBuilder.ts +13 -6
- package/src/unstable/httpapi/HttpApiClient.ts +5 -6
- package/src/unstable/httpapi/HttpApiEndpoint.ts +6 -7
- package/src/unstable/httpapi/HttpApiError.ts +43 -22
- package/src/unstable/httpapi/HttpApiMiddleware.ts +6 -5
- package/src/unstable/httpapi/OpenApi.ts +1 -1
- package/src/unstable/reactivity/AtomHttpApi.ts +14 -4
- package/src/unstable/sql/Statement.ts +19 -23
- package/src/unstable/workflow/Activity.ts +9 -9
- package/dist/internal/schema/to-codec.d.ts +0 -2
- package/dist/internal/schema/to-codec.d.ts.map +0 -1
- package/dist/internal/schema/to-codec.js +0 -126
- package/dist/internal/schema/to-codec.js.map +0 -1
- package/src/internal/schema/to-codec.ts +0 -138
|
@@ -16,7 +16,6 @@ import * as HttpRouter from "../http/HttpRouter.ts"
|
|
|
16
16
|
import type { HttpServerRequest } from "../http/HttpServerRequest.ts"
|
|
17
17
|
import type { HttpServerResponse } from "../http/HttpServerResponse.ts"
|
|
18
18
|
import type * as Multipart from "../http/Multipart.ts"
|
|
19
|
-
import { BadRequestFromSchemaError, type BadRequestNoContent } from "./HttpApiError.ts"
|
|
20
19
|
import type * as HttpApiGroup from "./HttpApiGroup.ts"
|
|
21
20
|
import type * as HttpApiMiddleware from "./HttpApiMiddleware.ts"
|
|
22
21
|
import * as HttpApiSchema from "./HttpApiSchema.ts"
|
|
@@ -54,7 +53,7 @@ export interface HttpApiEndpoint<
|
|
|
54
53
|
out Payload extends Schema.Top = never,
|
|
55
54
|
out Headers extends Schema.Top = never,
|
|
56
55
|
out Success extends Schema.Top = typeof HttpApiSchema.NoContent,
|
|
57
|
-
out Error extends Schema.Top =
|
|
56
|
+
out Error extends Schema.Top = never,
|
|
58
57
|
in out Middleware = never,
|
|
59
58
|
out MiddlewareR = never
|
|
60
59
|
> extends Pipeable {
|
|
@@ -172,7 +171,7 @@ export function getSuccessSchemas(endpoint: AnyWithProps): [Schema.Top, ...Array
|
|
|
172
171
|
}
|
|
173
172
|
|
|
174
173
|
/** @internal */
|
|
175
|
-
export function getErrorSchemas(endpoint: AnyWithProps):
|
|
174
|
+
export function getErrorSchemas(endpoint: AnyWithProps): Array<Schema.Top> {
|
|
176
175
|
const schemas = new Set<Schema.Top>(endpoint.error)
|
|
177
176
|
for (const middleware of endpoint.middlewares) {
|
|
178
177
|
const key = middleware as any as HttpApiMiddleware.AnyService
|
|
@@ -180,7 +179,7 @@ export function getErrorSchemas(endpoint: AnyWithProps): [Schema.Top, ...Array<S
|
|
|
180
179
|
schemas.add(schema)
|
|
181
180
|
}
|
|
182
181
|
}
|
|
183
|
-
return
|
|
182
|
+
return Array.from(schemas)
|
|
184
183
|
}
|
|
185
184
|
|
|
186
185
|
/**
|
|
@@ -951,7 +950,7 @@ export const make = <Method extends HttpMethod>(method: Method): {
|
|
|
951
950
|
: StringTree<ExtractSchemaOrArray<Payload>>,
|
|
952
951
|
StringTree<Headers extends Schema.Struct.Fields ? Schema.Struct<Headers> : Headers>,
|
|
953
952
|
Json<Success extends ReadonlyArray<Schema.Top> ? Success[number] : Success>,
|
|
954
|
-
Json<
|
|
953
|
+
Json<Error extends ReadonlyArray<Schema.Top> ? Error[number] : Error>
|
|
955
954
|
>
|
|
956
955
|
<
|
|
957
956
|
const Name extends string,
|
|
@@ -983,7 +982,7 @@ export const make = <Method extends HttpMethod>(method: Method): {
|
|
|
983
982
|
ExtractSchemaOrArray<Payload>,
|
|
984
983
|
ExtractSchemaOrArray<Headers>,
|
|
985
984
|
Success extends ReadonlyArray<Schema.Top> ? Success[number] : Success,
|
|
986
|
-
|
|
985
|
+
Error extends ReadonlyArray<Schema.Top> ? Error[number] : Error
|
|
987
986
|
>
|
|
988
987
|
} =>
|
|
989
988
|
<
|
|
@@ -1018,7 +1017,7 @@ export const make = <Method extends HttpMethod>(method: Method): {
|
|
|
1018
1017
|
: Payload,
|
|
1019
1018
|
Headers extends Schema.Struct.Fields ? Schema.Struct<Headers> : Headers,
|
|
1020
1019
|
Success extends ReadonlyArray<Schema.Top> ? Success[number] : Success,
|
|
1021
|
-
|
|
1020
|
+
Error extends ReadonlyArray<Schema.Top> ? Error[number] : Error
|
|
1022
1021
|
> => {
|
|
1023
1022
|
const disableCodecs = options?.disableCodecs ?? false
|
|
1024
1023
|
const transformStringTree = disableCodecs ? identity : Schema.toCodecStringTree
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @since 4.0.0
|
|
3
3
|
*/
|
|
4
|
+
import * as Data from "../../Data.ts"
|
|
4
5
|
import * as Effect from "../../Effect.ts"
|
|
5
6
|
import * as ErrorReporter from "../../ErrorReporter.ts"
|
|
6
|
-
import {
|
|
7
|
+
import { hasProperty } from "../../Predicate.ts"
|
|
7
8
|
import * as Schema from "../../Schema.ts"
|
|
8
|
-
import * as Transformation from "../../SchemaTransformation.ts"
|
|
9
9
|
import * as HttpServerRespondable from "../http/HttpServerRespondable.ts"
|
|
10
10
|
import * as HttpServerResponse from "../http/HttpServerResponse.ts"
|
|
11
11
|
import * as HttpApiSchema from "./HttpApiSchema.ts"
|
|
@@ -48,26 +48,6 @@ export const BadRequestNoContent = BadRequest.pipe(HttpApiSchema.asNoContent({
|
|
|
48
48
|
decode: () => new BadRequest({})
|
|
49
49
|
}))
|
|
50
50
|
|
|
51
|
-
/**
|
|
52
|
-
* @category Built-in errors
|
|
53
|
-
* @since 4.0.0
|
|
54
|
-
*/
|
|
55
|
-
export const BadRequestFromSchemaError = BadRequest.pipe(
|
|
56
|
-
Schema.decodeTo(
|
|
57
|
-
Schema.Union([Schema.declare(Schema.isSchemaError), BadRequest]),
|
|
58
|
-
Transformation.transform({
|
|
59
|
-
encode: (_) => BadRequest.singleton,
|
|
60
|
-
decode: identity
|
|
61
|
-
})
|
|
62
|
-
),
|
|
63
|
-
HttpApiSchema.asNoContent({
|
|
64
|
-
decode: () => new BadRequest({})
|
|
65
|
-
})
|
|
66
|
-
).annotate({
|
|
67
|
-
httpApiStatus: 400,
|
|
68
|
-
description: "BadRequest"
|
|
69
|
-
})
|
|
70
|
-
|
|
71
51
|
/**
|
|
72
52
|
* @category Built-in errors
|
|
73
53
|
* @since 4.0.0
|
|
@@ -332,3 +312,44 @@ export class ServiceUnavailable
|
|
|
332
312
|
export const ServiceUnavailableNoContent = ServiceUnavailable.pipe(HttpApiSchema.asNoContent({
|
|
333
313
|
decode: () => new ServiceUnavailable({})
|
|
334
314
|
}))
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* @category Parsing errors
|
|
318
|
+
* @since 4.0.0
|
|
319
|
+
*/
|
|
320
|
+
export type HttpApiSchemaErrorTypeId = "~effect/httpapi/HttpApiError/HttpApiSchemaError"
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* @category Parsing errors
|
|
324
|
+
* @since 4.0.0
|
|
325
|
+
*/
|
|
326
|
+
export const HttpApiSchemaErrorTypeId: HttpApiSchemaErrorTypeId = "~effect/httpapi/HttpApiError/HttpApiSchemaError"
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* @category Parsing errors
|
|
330
|
+
* @since 4.0.0
|
|
331
|
+
*/
|
|
332
|
+
export class HttpApiSchemaError extends Data.TaggedClass("HttpApiSchemaError")<{
|
|
333
|
+
readonly kind: "Params" | "Headers" | "Query" | "Body" | "Payload"
|
|
334
|
+
readonly cause: Schema.SchemaError
|
|
335
|
+
}> {
|
|
336
|
+
readonly [HttpApiSchemaErrorTypeId]: HttpApiSchemaErrorTypeId = HttpApiSchemaErrorTypeId
|
|
337
|
+
|
|
338
|
+
static is(u: unknown): u is HttpApiSchemaError {
|
|
339
|
+
return hasProperty(u, HttpApiSchemaErrorTypeId)
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
static wrap<A, R>(
|
|
343
|
+
kind: HttpApiSchemaError["kind"],
|
|
344
|
+
effect: Effect.Effect<A, Schema.SchemaError, R>
|
|
345
|
+
): Effect.Effect<A, HttpApiSchemaError, R> {
|
|
346
|
+
return Effect.mapError(effect, (error) => new HttpApiSchemaError({ kind, cause: error }))
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
readonly name = "HttpApiSchemaError"
|
|
350
|
+
readonly message = this.kind;
|
|
351
|
+
|
|
352
|
+
[HttpServerRespondable.symbol]() {
|
|
353
|
+
return Effect.succeed(badRequestResponse)
|
|
354
|
+
}
|
|
355
|
+
}
|
|
@@ -5,7 +5,7 @@ import * as Context from "../../Context.ts"
|
|
|
5
5
|
import * as Effect from "../../Effect.ts"
|
|
6
6
|
import * as Layer from "../../Layer.ts"
|
|
7
7
|
import { hasProperty } from "../../Predicate.ts"
|
|
8
|
-
import * as Schema from "../../Schema.ts"
|
|
8
|
+
import type * as Schema from "../../Schema.ts"
|
|
9
9
|
import { Scope } from "../../Scope.ts"
|
|
10
10
|
import type { unhandled } from "../../Types.ts"
|
|
11
11
|
import type * as HttpClientError from "../http/HttpClientError.ts"
|
|
@@ -14,6 +14,7 @@ import type * as HttpClientResponse from "../http/HttpClientResponse.ts"
|
|
|
14
14
|
import type * as HttpRouter from "../http/HttpRouter.ts"
|
|
15
15
|
import type { HttpServerResponse } from "../http/HttpServerResponse.ts"
|
|
16
16
|
import type * as HttpApiEndpoint from "./HttpApiEndpoint.ts"
|
|
17
|
+
import { HttpApiSchemaError } from "./HttpApiError.ts"
|
|
17
18
|
import type * as HttpApiGroup from "./HttpApiGroup.ts"
|
|
18
19
|
import type * as HttpApiSecurity from "./HttpApiSecurity.ts"
|
|
19
20
|
|
|
@@ -333,11 +334,11 @@ function getError(error: ErrorConstraint | undefined): ReadonlySet<Schema.Top> {
|
|
|
333
334
|
export const layerSchemaErrorTransform = <Id, E extends ErrorConstraint, Requires>(
|
|
334
335
|
service: Context.Service<Id, HttpApiMiddleware<never, E, Requires>>,
|
|
335
336
|
transform: (
|
|
336
|
-
error:
|
|
337
|
+
error: HttpApiSchemaError,
|
|
337
338
|
context: { readonly endpoint: HttpApiEndpoint.AnyWithProps; readonly group: HttpApiGroup.AnyWithProps }
|
|
338
339
|
) => Effect.Effect<
|
|
339
340
|
HttpServerResponse,
|
|
340
|
-
ErrorSchemaFromConstraint<E>["Type"] |
|
|
341
|
+
ErrorSchemaFromConstraint<E>["Type"] | HttpApiSchemaError,
|
|
341
342
|
Requires | HttpRouter.Provided
|
|
342
343
|
>
|
|
343
344
|
): Layer.Layer<Id> =>
|
|
@@ -348,9 +349,9 @@ export const layerSchemaErrorTransform = <Id, E extends ErrorConstraint, Require
|
|
|
348
349
|
httpEffect,
|
|
349
350
|
(e): Effect.Effect<
|
|
350
351
|
HttpServerResponse,
|
|
351
|
-
unhandled |
|
|
352
|
+
unhandled | HttpApiSchemaError | ErrorSchemaFromConstraint<E>["Type"],
|
|
352
353
|
Requires | HttpRouter.Provided
|
|
353
|
-
> =>
|
|
354
|
+
> => HttpApiSchemaError.is(e) ? transform(e, options) : Effect.fail(e)
|
|
354
355
|
)
|
|
355
356
|
)
|
|
356
357
|
|
|
@@ -524,7 +524,7 @@ type ResponseBodies = Map<
|
|
|
524
524
|
>
|
|
525
525
|
|
|
526
526
|
function extractResponseBodies(
|
|
527
|
-
schemas:
|
|
527
|
+
schemas: Array<Schema.Top>,
|
|
528
528
|
getStatus: (ast: AST.AST) => number,
|
|
529
529
|
getDescription: (ast: AST.AST) => string | undefined
|
|
530
530
|
): ResponseBodies {
|
|
@@ -16,6 +16,7 @@ import type * as HttpApi from "../httpapi/HttpApi.ts"
|
|
|
16
16
|
import * as HttpApiClient from "../httpapi/HttpApiClient.ts"
|
|
17
17
|
import * as HttpApiEndpoint from "../httpapi/HttpApiEndpoint.ts"
|
|
18
18
|
import type * as HttpApiGroup from "../httpapi/HttpApiGroup.ts"
|
|
19
|
+
import type * as HttpApiMiddleware from "../httpapi/HttpApiMiddleware.ts"
|
|
19
20
|
import * as AsyncResult from "./AsyncResult.ts"
|
|
20
21
|
import * as Atom from "./Atom.ts"
|
|
21
22
|
import * as Reactivity from "./Reactivity.ts"
|
|
@@ -57,7 +58,7 @@ export interface AtomHttpApiClient<Self, Id extends string, Groups extends HttpA
|
|
|
57
58
|
infer _Headers,
|
|
58
59
|
infer _Success,
|
|
59
60
|
infer _Error,
|
|
60
|
-
infer
|
|
61
|
+
infer _Middleware,
|
|
61
62
|
infer _RE
|
|
62
63
|
>
|
|
63
64
|
] ? Atom.AtomResultFn<
|
|
@@ -67,7 +68,7 @@ export interface AtomHttpApiClient<Self, Id extends string, Groups extends HttpA
|
|
|
67
68
|
}
|
|
68
69
|
>,
|
|
69
70
|
ResponseByMode<_Success["Type"], ResponseMode>,
|
|
70
|
-
_Error
|
|
71
|
+
ErrorByMode<_Error, _Middleware, ResponseMode>
|
|
71
72
|
>
|
|
72
73
|
: never
|
|
73
74
|
|
|
@@ -119,13 +120,13 @@ export interface AtomHttpApiClient<Self, Id extends string, Groups extends HttpA
|
|
|
119
120
|
infer _Headers,
|
|
120
121
|
infer _Success,
|
|
121
122
|
infer _Error,
|
|
122
|
-
infer
|
|
123
|
+
infer _Middleware,
|
|
123
124
|
infer _RE
|
|
124
125
|
>
|
|
125
126
|
] ? Atom.Atom<
|
|
126
127
|
AsyncResult.AsyncResult<
|
|
127
128
|
ResponseByMode<_Success["Type"], ResponseMode>,
|
|
128
|
-
_Error
|
|
129
|
+
ErrorByMode<_Error, _Middleware, ResponseMode>
|
|
129
130
|
>
|
|
130
131
|
>
|
|
131
132
|
: never
|
|
@@ -316,3 +317,12 @@ type ResponseByMode<Success, ResponseMode extends HttpApiEndpoint.ClientResponse
|
|
|
316
317
|
["decoded-and-response"] ? [Success, HttpClientResponse]
|
|
317
318
|
: [ResponseMode] extends ["response-only"] ? HttpClientResponse
|
|
318
319
|
: Success
|
|
320
|
+
|
|
321
|
+
type ErrorByMode<
|
|
322
|
+
Error extends Schema.Top,
|
|
323
|
+
Middleware,
|
|
324
|
+
ResponseMode extends HttpApiEndpoint.ClientResponseMode
|
|
325
|
+
> =
|
|
326
|
+
| HttpApiMiddleware.Error<Middleware>
|
|
327
|
+
| HttpApiMiddleware.ClientError<Middleware>
|
|
328
|
+
| ([ResponseMode] extends ["response-only"] ? never : Error["Type"])
|
|
@@ -4,12 +4,10 @@
|
|
|
4
4
|
import { Clock } from "../../Clock.ts"
|
|
5
5
|
import * as Context from "../../Context.ts"
|
|
6
6
|
import * as Effect from "../../Effect.ts"
|
|
7
|
+
import * as Effectable from "../../Effectable.ts"
|
|
7
8
|
import type * as Fiber from "../../Fiber.ts"
|
|
8
9
|
import { constUndefined } from "../../Function.ts"
|
|
9
|
-
import type { Inspectable } from "../../Inspectable.ts"
|
|
10
|
-
import * as core from "../../internal/core.ts"
|
|
11
10
|
import * as internalEffect from "../../internal/effect.ts"
|
|
12
|
-
import type { Pipeable } from "../../Pipeable.ts"
|
|
13
11
|
import { hasProperty } from "../../Predicate.ts"
|
|
14
12
|
import { TracerTimingEnabled } from "../../References.ts"
|
|
15
13
|
import * as Stream from "../../Stream.ts"
|
|
@@ -49,7 +47,7 @@ export type Dialect = "sqlite" | "pg" | "mysql" | "mssql" | "clickhouse"
|
|
|
49
47
|
* @category model
|
|
50
48
|
* @since 4.0.0
|
|
51
49
|
*/
|
|
52
|
-
export interface Statement<A> extends Fragment, Effect.Effect<ReadonlyArray<A>, SqlError
|
|
50
|
+
export interface Statement<A> extends Fragment, Effect.Effect<ReadonlyArray<A>, SqlError> {
|
|
53
51
|
readonly raw: Effect.Effect<unknown, SqlError>
|
|
54
52
|
readonly withoutTransform: Effect.Effect<ReadonlyArray<A>, SqlError>
|
|
55
53
|
readonly stream: Stream.Stream<A, SqlError>
|
|
@@ -1128,8 +1126,23 @@ const StatementProto: Omit<
|
|
|
1128
1126
|
StatementImpl<any>,
|
|
1129
1127
|
"segments" | "acquirer" | "compiler" | "spanAttributes" | "transformRows"
|
|
1130
1128
|
> = {
|
|
1131
|
-
...
|
|
1132
|
-
|
|
1129
|
+
...Effectable.Prototype<StatementImpl<any>>({
|
|
1130
|
+
label: "Statement",
|
|
1131
|
+
evaluate(fiber) {
|
|
1132
|
+
const span = internalEffect.makeSpanUnsafe(fiber, "sql.execute", { kind: "client" })
|
|
1133
|
+
const clock = fiber.getRef(Clock)
|
|
1134
|
+
const timingEnabled = fiber.getRef(TracerTimingEnabled)
|
|
1135
|
+
return Effect.onExit(
|
|
1136
|
+
this.withConnectionSpan(
|
|
1137
|
+
"execute",
|
|
1138
|
+
(connection, sql, params) => connection.execute(sql, params, this.transformRows),
|
|
1139
|
+
false,
|
|
1140
|
+
span
|
|
1141
|
+
),
|
|
1142
|
+
(exit) => internalEffect.endSpan(span, exit, clock, timingEnabled)
|
|
1143
|
+
)
|
|
1144
|
+
}
|
|
1145
|
+
}),
|
|
1133
1146
|
[FragmentTypeId]: FragmentTypeId,
|
|
1134
1147
|
withConnection<XA, E>(
|
|
1135
1148
|
this: StatementImpl<any>,
|
|
@@ -1229,23 +1242,6 @@ const StatementProto: Omit<
|
|
|
1229
1242
|
) {
|
|
1230
1243
|
return this.compiler.compile(this, withoutTransform ?? false)
|
|
1231
1244
|
},
|
|
1232
|
-
[core.evaluate as any](
|
|
1233
|
-
this: StatementImpl<any>,
|
|
1234
|
-
fiber: Fiber.Fiber<any, any>
|
|
1235
|
-
): Effect.Effect<ReadonlyArray<any>, SqlError> {
|
|
1236
|
-
const span = internalEffect.makeSpanUnsafe(fiber, "sql.execute", { kind: "client" })
|
|
1237
|
-
const clock = fiber.getRef(Clock)
|
|
1238
|
-
const timingEnabled = fiber.getRef(TracerTimingEnabled)
|
|
1239
|
-
return Effect.onExit(
|
|
1240
|
-
this.withConnectionSpan(
|
|
1241
|
-
"execute",
|
|
1242
|
-
(connection, sql, params) => connection.execute(sql, params, this.transformRows),
|
|
1243
|
-
false,
|
|
1244
|
-
span
|
|
1245
|
-
),
|
|
1246
|
-
(exit) => internalEffect.endSpan(span, exit, clock, timingEnabled)
|
|
1247
|
-
)
|
|
1248
|
-
},
|
|
1249
1245
|
toJSON(this: StatementImpl<any>) {
|
|
1250
1246
|
const [sql, params] = this.compile()
|
|
1251
1247
|
return {
|
|
@@ -5,8 +5,8 @@ import type { NonEmptyReadonlyArray } from "../../Array.ts"
|
|
|
5
5
|
import * as Cause from "../../Cause.ts"
|
|
6
6
|
import * as Context from "../../Context.ts"
|
|
7
7
|
import * as Effect from "../../Effect.ts"
|
|
8
|
+
import * as Effectable from "../../Effectable.ts"
|
|
8
9
|
import { dual } from "../../Function.ts"
|
|
9
|
-
import { PipeInspectableProto, YieldableProto } from "../../internal/core.ts"
|
|
10
10
|
import * as Schedule from "../../Schedule.ts"
|
|
11
11
|
import * as Schema from "../../Schema.ts"
|
|
12
12
|
import type { Scope } from "../../Scope.ts"
|
|
@@ -27,8 +27,7 @@ export interface Activity<
|
|
|
27
27
|
Error extends Schema.Top = Schema.Never,
|
|
28
28
|
R = never
|
|
29
29
|
> extends
|
|
30
|
-
Effect.
|
|
31
|
-
Activity<Success, Error, R>,
|
|
30
|
+
Effect.Effect<
|
|
32
31
|
Success["Type"],
|
|
33
32
|
Error["Type"],
|
|
34
33
|
Success["DecodingServices"] | Error["DecodingServices"] | R | WorkflowEngine | WorkflowInstance
|
|
@@ -123,8 +122,12 @@ export const make = <
|
|
|
123
122
|
options.interruptRetryPolicy
|
|
124
123
|
)(options.execute)
|
|
125
124
|
const self: Activity<Success, Error, Exclude<R, WorkflowInstance | WorkflowEngine>> = {
|
|
126
|
-
...
|
|
127
|
-
|
|
125
|
+
...Effectable.Prototype<Activity<Success, Error, R>>({
|
|
126
|
+
label: "Activity",
|
|
127
|
+
evaluate(_) {
|
|
128
|
+
return execute
|
|
129
|
+
}
|
|
130
|
+
}),
|
|
128
131
|
[TypeId]: TypeId,
|
|
129
132
|
name: options.name,
|
|
130
133
|
successSchema,
|
|
@@ -147,10 +150,7 @@ export const make = <
|
|
|
147
150
|
executeEncoded: Effect.matchEffect(executeWithoutInterrupt, {
|
|
148
151
|
onFailure: (error) => Effect.flatMap(Effect.orDie(Schema.encodeEffect(errorSchemaJson)(error)), Effect.fail),
|
|
149
152
|
onSuccess: (value) => Effect.orDie(Schema.encodeEffect(successSchemaJson)(value))
|
|
150
|
-
})
|
|
151
|
-
asEffect() {
|
|
152
|
-
return execute
|
|
153
|
-
}
|
|
153
|
+
})
|
|
154
154
|
} as any
|
|
155
155
|
execute = makeExecute(self)
|
|
156
156
|
return self
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"to-codec.d.ts","sourceRoot":"","sources":["../../../src/internal/schema/to-codec.ts"],"names":[],"mappings":""}
|
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import { memoize } from "../../Function.js";
|
|
2
|
-
import * as Predicate from "../../Predicate.js";
|
|
3
|
-
import * as AST from "../../SchemaAST.js";
|
|
4
|
-
import * as InternalSchema from "./schema.js";
|
|
5
|
-
/** @internal */
|
|
6
|
-
export const toCodecJson = /*#__PURE__*/AST.toCodec(ast => {
|
|
7
|
-
const out = toCodecJsonBase(ast);
|
|
8
|
-
if (out !== ast && AST.isOptional(ast)) {
|
|
9
|
-
return AST.optionalKeyLastLink(out);
|
|
10
|
-
}
|
|
11
|
-
return out;
|
|
12
|
-
});
|
|
13
|
-
function toCodecJsonBase(ast) {
|
|
14
|
-
switch (ast._tag) {
|
|
15
|
-
case "Declaration":
|
|
16
|
-
{
|
|
17
|
-
const getLink = ast.annotations?.toCodecJson ?? ast.annotations?.toCodec;
|
|
18
|
-
if (Predicate.isFunction(getLink)) {
|
|
19
|
-
const tps = AST.isDeclaration(ast) ? ast.typeParameters.map(tp => InternalSchema.make(AST.toEncoded(tp))) : [];
|
|
20
|
-
const link = getLink(tps);
|
|
21
|
-
const to = toCodecJson(link.to);
|
|
22
|
-
return AST.replaceEncoding(ast, to === link.to ? [link] : [new AST.Link(to, link.transformation)]);
|
|
23
|
-
}
|
|
24
|
-
return AST.replaceEncoding(ast, [AST.unknownToNull]);
|
|
25
|
-
}
|
|
26
|
-
case "Unknown":
|
|
27
|
-
case "ObjectKeyword":
|
|
28
|
-
return AST.replaceEncoding(ast, [AST.unknownToJson]);
|
|
29
|
-
case "Undefined":
|
|
30
|
-
case "Void":
|
|
31
|
-
case "Literal":
|
|
32
|
-
case "Number":
|
|
33
|
-
return ast.toCodecJson();
|
|
34
|
-
case "UniqueSymbol":
|
|
35
|
-
case "Symbol":
|
|
36
|
-
case "BigInt":
|
|
37
|
-
return ast.toCodecStringTree();
|
|
38
|
-
case "Objects":
|
|
39
|
-
{
|
|
40
|
-
if (ast.propertySignatures.some(ps => typeof ps.name !== "string")) {
|
|
41
|
-
throw new globalThis.Error("Objects property names must be strings", {
|
|
42
|
-
cause: ast
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
return ast.recur(toCodecJson);
|
|
46
|
-
}
|
|
47
|
-
case "Union":
|
|
48
|
-
{
|
|
49
|
-
const sortedTypes = jsonReorder(ast.types);
|
|
50
|
-
if (sortedTypes !== ast.types) {
|
|
51
|
-
return new AST.Union(sortedTypes, ast.mode, ast.annotations, ast.checks, ast.encoding, ast.context).recur(toCodecJson);
|
|
52
|
-
}
|
|
53
|
-
return ast.recur(toCodecJson);
|
|
54
|
-
}
|
|
55
|
-
case "Arrays":
|
|
56
|
-
case "Suspend":
|
|
57
|
-
return ast.recur(toCodecJson);
|
|
58
|
-
}
|
|
59
|
-
// `Schema.Any` is used as an escape hatch
|
|
60
|
-
return ast;
|
|
61
|
-
}
|
|
62
|
-
/** @internal */
|
|
63
|
-
export const jsonReorder = /*#__PURE__*/makeReorder(getJsonPriority);
|
|
64
|
-
function getJsonPriority(ast) {
|
|
65
|
-
switch (ast._tag) {
|
|
66
|
-
case "BigInt":
|
|
67
|
-
case "Symbol":
|
|
68
|
-
case "UniqueSymbol":
|
|
69
|
-
return 0;
|
|
70
|
-
default:
|
|
71
|
-
return 1;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
/** @internal */
|
|
75
|
-
export function makeReorder(getPriority) {
|
|
76
|
-
return types => {
|
|
77
|
-
// Create a map of original indices for O(1) lookup
|
|
78
|
-
const indexMap = new Map();
|
|
79
|
-
for (let i = 0; i < types.length; i++) {
|
|
80
|
-
indexMap.set(AST.toEncoded(types[i]), i);
|
|
81
|
-
}
|
|
82
|
-
// Create a sorted copy of the types array
|
|
83
|
-
const sortedTypes = [...types].sort((a, b) => {
|
|
84
|
-
a = AST.toEncoded(a);
|
|
85
|
-
b = AST.toEncoded(b);
|
|
86
|
-
const pa = getPriority(a);
|
|
87
|
-
const pb = getPriority(b);
|
|
88
|
-
if (pa !== pb) return pa - pb;
|
|
89
|
-
// If priorities are equal, maintain original order (stable sort)
|
|
90
|
-
return indexMap.get(a) - indexMap.get(b);
|
|
91
|
-
});
|
|
92
|
-
// Check if order changed by comparing arrays
|
|
93
|
-
const orderChanged = sortedTypes.some((ast, index) => ast !== types[index]);
|
|
94
|
-
if (!orderChanged) return types;
|
|
95
|
-
return sortedTypes;
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
/** @internal */
|
|
99
|
-
export const toCodecIso = /*#__PURE__*/memoize(ast => {
|
|
100
|
-
const out = toCodecIsoBase(ast);
|
|
101
|
-
if (out !== ast && AST.isOptional(ast)) {
|
|
102
|
-
return AST.optionalKeyLastLink(out);
|
|
103
|
-
}
|
|
104
|
-
return out;
|
|
105
|
-
});
|
|
106
|
-
function toCodecIsoBase(ast) {
|
|
107
|
-
switch (ast._tag) {
|
|
108
|
-
case "Declaration":
|
|
109
|
-
{
|
|
110
|
-
const getLink = ast.annotations?.toCodecIso ?? ast.annotations?.toCodec;
|
|
111
|
-
if (Predicate.isFunction(getLink)) {
|
|
112
|
-
const link = getLink(ast.typeParameters.map(tp => InternalSchema.make(tp)));
|
|
113
|
-
const to = toCodecIso(link.to);
|
|
114
|
-
return AST.replaceEncoding(ast, to === link.to ? [link] : [new AST.Link(to, link.transformation)]);
|
|
115
|
-
}
|
|
116
|
-
return ast;
|
|
117
|
-
}
|
|
118
|
-
case "Arrays":
|
|
119
|
-
case "Objects":
|
|
120
|
-
case "Union":
|
|
121
|
-
case "Suspend":
|
|
122
|
-
return ast.recur(toCodecIso);
|
|
123
|
-
}
|
|
124
|
-
return ast;
|
|
125
|
-
}
|
|
126
|
-
//# sourceMappingURL=to-codec.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"to-codec.js","names":["memoize","Predicate","AST","InternalSchema","toCodecJson","toCodec","ast","out","toCodecJsonBase","isOptional","optionalKeyLastLink","_tag","getLink","annotations","isFunction","tps","isDeclaration","typeParameters","map","tp","make","toEncoded","link","to","replaceEncoding","Link","transformation","unknownToNull","unknownToJson","toCodecStringTree","propertySignatures","some","ps","name","globalThis","Error","cause","recur","sortedTypes","jsonReorder","types","Union","mode","checks","encoding","context","makeReorder","getJsonPriority","getPriority","indexMap","Map","i","length","set","sort","a","b","pa","pb","get","orderChanged","index","toCodecIso","toCodecIsoBase"],"sources":["../../../src/internal/schema/to-codec.ts"],"sourcesContent":[null],"mappings":"AAAA,SAASA,OAAO,QAAQ,mBAAmB;AAC3C,OAAO,KAAKC,SAAS,MAAM,oBAAoB;AAC/C,OAAO,KAAKC,GAAG,MAAM,oBAAoB;AACzC,OAAO,KAAKC,cAAc,MAAM,aAAa;AAE7C;AACA,OAAO,MAAMC,WAAW,gBAAGF,GAAG,CAACG,OAAO,CAAEC,GAAG,IAAI;EAC7C,MAAMC,GAAG,GAAGC,eAAe,CAACF,GAAG,CAAC;EAChC,IAAIC,GAAG,KAAKD,GAAG,IAAIJ,GAAG,CAACO,UAAU,CAACH,GAAG,CAAC,EAAE;IACtC,OAAOJ,GAAG,CAACQ,mBAAmB,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ACA,OAAOA,GAAG;AACZ,CAAC,CAAC;AAEF,SAASwD,cAAcA,CAACzD,GAAY;EAClC,QAAQA,GAAG,CAACK,IAAI;IACd,KAAK,aAAa;MAAE;QAClB,MAAMC,OAAO,GAAGN,GAAG,CAACO,WAAW,EAAEiD,UAAU,IAAIxD,GAAG,CAACO,WAAW,EAAER,OAAO;QACvE,IAAIJ,SAAS,CAACa,UAAU,CAACF,OAAO,CAAC,EAAE;UACjC,MAAMU,IAAI,GAAGV,OAAO,CAACN,GAAG,CAACW,cAAc,CAACC,GAAG,CAAEC,EAAE,IAAKhB,cAAc,CAACiB,IAAI,CAACD,EAAE,CAAC,CAAC,CAAC;UAC7E,MAAMI,EAAE,GAAGuC,UAAU,CAACxC,IAAI,CAACC,EAAE,CAAC;UAC9B,OAAOrB,GAAG,CAACsB,eAAe,CAAClB,GAAG,EAAEiB,EAAE,KAAKD,IAAI,CAACC,EAAE,GAAG,CAACD,IAAI,CAAC,GAAG,CAAC,IAAIpB,GAAG,CAACuB,IAAI,CAACF,EAAE,EAAED,IAAI,CAACI,cAAc,CAAC,CAAC,CAAC;QACpG;QACA,OAAOpB,GAAG;MACZ;IACA,KAAK,QAAQ;IACb,KAAK,SAAS;IACd,KAAK,OAAO;IACZ,KAAK,SAAS;MACZ,OAAOA,GAAG,CAAC+B,KAAK,CAACyB,UAAU,CAAC;EAChC;EACA,OAAOxD,GAAG;AACZ","ignoreList":[]}
|
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
import { memoize } from "../../Function.ts"
|
|
2
|
-
import * as Predicate from "../../Predicate.ts"
|
|
3
|
-
import * as AST from "../../SchemaAST.ts"
|
|
4
|
-
import * as InternalSchema from "./schema.ts"
|
|
5
|
-
|
|
6
|
-
/** @internal */
|
|
7
|
-
export const toCodecJson = AST.toCodec((ast) => {
|
|
8
|
-
const out = toCodecJsonBase(ast)
|
|
9
|
-
if (out !== ast && AST.isOptional(ast)) {
|
|
10
|
-
return AST.optionalKeyLastLink(out)
|
|
11
|
-
}
|
|
12
|
-
return out
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
function toCodecJsonBase(ast: AST.AST): AST.AST {
|
|
16
|
-
switch (ast._tag) {
|
|
17
|
-
case "Declaration": {
|
|
18
|
-
const getLink = ast.annotations?.toCodecJson ?? ast.annotations?.toCodec
|
|
19
|
-
if (Predicate.isFunction(getLink)) {
|
|
20
|
-
const tps = AST.isDeclaration(ast)
|
|
21
|
-
? ast.typeParameters.map((tp) => InternalSchema.make(AST.toEncoded(tp)))
|
|
22
|
-
: []
|
|
23
|
-
const link = getLink(tps)
|
|
24
|
-
const to = toCodecJson(link.to)
|
|
25
|
-
return AST.replaceEncoding(ast, to === link.to ? [link] : [new AST.Link(to, link.transformation)])
|
|
26
|
-
}
|
|
27
|
-
return AST.replaceEncoding(ast, [AST.unknownToNull])
|
|
28
|
-
}
|
|
29
|
-
case "Unknown":
|
|
30
|
-
case "ObjectKeyword":
|
|
31
|
-
return AST.replaceEncoding(ast, [AST.unknownToJson])
|
|
32
|
-
case "Undefined":
|
|
33
|
-
case "Void":
|
|
34
|
-
case "Literal":
|
|
35
|
-
case "Number":
|
|
36
|
-
return ast.toCodecJson()
|
|
37
|
-
case "UniqueSymbol":
|
|
38
|
-
case "Symbol":
|
|
39
|
-
case "BigInt":
|
|
40
|
-
return ast.toCodecStringTree()
|
|
41
|
-
case "Objects": {
|
|
42
|
-
if (ast.propertySignatures.some((ps) => typeof ps.name !== "string")) {
|
|
43
|
-
throw new globalThis.Error("Objects property names must be strings", { cause: ast })
|
|
44
|
-
}
|
|
45
|
-
return ast.recur(toCodecJson)
|
|
46
|
-
}
|
|
47
|
-
case "Union": {
|
|
48
|
-
const sortedTypes = jsonReorder(ast.types)
|
|
49
|
-
if (sortedTypes !== ast.types) {
|
|
50
|
-
return new AST.Union(
|
|
51
|
-
sortedTypes,
|
|
52
|
-
ast.mode,
|
|
53
|
-
ast.annotations,
|
|
54
|
-
ast.checks,
|
|
55
|
-
ast.encoding,
|
|
56
|
-
ast.context
|
|
57
|
-
).recur(toCodecJson)
|
|
58
|
-
}
|
|
59
|
-
return ast.recur(toCodecJson)
|
|
60
|
-
}
|
|
61
|
-
case "Arrays":
|
|
62
|
-
case "Suspend":
|
|
63
|
-
return ast.recur(toCodecJson)
|
|
64
|
-
}
|
|
65
|
-
// `Schema.Any` is used as an escape hatch
|
|
66
|
-
return ast
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/** @internal */
|
|
70
|
-
export const jsonReorder = makeReorder(getJsonPriority)
|
|
71
|
-
|
|
72
|
-
function getJsonPriority(ast: AST.AST): number {
|
|
73
|
-
switch (ast._tag) {
|
|
74
|
-
case "BigInt":
|
|
75
|
-
case "Symbol":
|
|
76
|
-
case "UniqueSymbol":
|
|
77
|
-
return 0
|
|
78
|
-
default:
|
|
79
|
-
return 1
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/** @internal */
|
|
84
|
-
export function makeReorder(getPriority: (ast: AST.AST) => number) {
|
|
85
|
-
return (types: ReadonlyArray<AST.AST>): ReadonlyArray<AST.AST> => {
|
|
86
|
-
// Create a map of original indices for O(1) lookup
|
|
87
|
-
const indexMap = new Map<AST.AST, number>()
|
|
88
|
-
for (let i = 0; i < types.length; i++) {
|
|
89
|
-
indexMap.set(AST.toEncoded(types[i]), i)
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// Create a sorted copy of the types array
|
|
93
|
-
const sortedTypes = [...types].sort((a, b) => {
|
|
94
|
-
a = AST.toEncoded(a)
|
|
95
|
-
b = AST.toEncoded(b)
|
|
96
|
-
const pa = getPriority(a)
|
|
97
|
-
const pb = getPriority(b)
|
|
98
|
-
if (pa !== pb) return pa - pb
|
|
99
|
-
// If priorities are equal, maintain original order (stable sort)
|
|
100
|
-
return indexMap.get(a)! - indexMap.get(b)!
|
|
101
|
-
})
|
|
102
|
-
|
|
103
|
-
// Check if order changed by comparing arrays
|
|
104
|
-
const orderChanged = sortedTypes.some((ast, index) => ast !== types[index])
|
|
105
|
-
|
|
106
|
-
if (!orderChanged) return types
|
|
107
|
-
return sortedTypes
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/** @internal */
|
|
112
|
-
export const toCodecIso = memoize((ast: AST.AST): AST.AST => {
|
|
113
|
-
const out = toCodecIsoBase(ast)
|
|
114
|
-
if (out !== ast && AST.isOptional(ast)) {
|
|
115
|
-
return AST.optionalKeyLastLink(out)
|
|
116
|
-
}
|
|
117
|
-
return out
|
|
118
|
-
})
|
|
119
|
-
|
|
120
|
-
function toCodecIsoBase(ast: AST.AST): AST.AST {
|
|
121
|
-
switch (ast._tag) {
|
|
122
|
-
case "Declaration": {
|
|
123
|
-
const getLink = ast.annotations?.toCodecIso ?? ast.annotations?.toCodec
|
|
124
|
-
if (Predicate.isFunction(getLink)) {
|
|
125
|
-
const link = getLink(ast.typeParameters.map((tp) => InternalSchema.make(tp)))
|
|
126
|
-
const to = toCodecIso(link.to)
|
|
127
|
-
return AST.replaceEncoding(ast, to === link.to ? [link] : [new AST.Link(to, link.transformation)])
|
|
128
|
-
}
|
|
129
|
-
return ast
|
|
130
|
-
}
|
|
131
|
-
case "Arrays":
|
|
132
|
-
case "Objects":
|
|
133
|
-
case "Union":
|
|
134
|
-
case "Suspend":
|
|
135
|
-
return ast.recur(toCodecIso)
|
|
136
|
-
}
|
|
137
|
-
return ast
|
|
138
|
-
}
|