@effect/platform 0.11.0 → 0.11.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.
Files changed (113) hide show
  1. package/Http/Body.d.ts +129 -0
  2. package/Http/Body.d.ts.map +1 -0
  3. package/Http/Body.js +70 -0
  4. package/Http/Body.js.map +1 -0
  5. package/Http/Client.d.ts +213 -0
  6. package/Http/Client.d.ts.map +1 -0
  7. package/Http/Client.js +124 -0
  8. package/Http/Client.js.map +1 -0
  9. package/Http/ClientError.d.ts +70 -0
  10. package/Http/ClientError.d.ts.map +1 -0
  11. package/Http/ClientError.js +28 -0
  12. package/Http/ClientError.js.map +1 -0
  13. package/Http/ClientRequest.d.ts +282 -0
  14. package/Http/ClientRequest.d.ts.map +1 -0
  15. package/Http/ClientRequest.js +202 -0
  16. package/Http/ClientRequest.js.map +1 -0
  17. package/Http/ClientResponse.d.ts +36 -0
  18. package/Http/ClientResponse.d.ts.map +1 -0
  19. package/Http/ClientResponse.js +29 -0
  20. package/Http/ClientResponse.js.map +1 -0
  21. package/Http/FormData.d.ts +56 -0
  22. package/Http/FormData.d.ts.map +1 -0
  23. package/Http/FormData.js +13 -0
  24. package/Http/FormData.js.map +1 -0
  25. package/Http/Headers.d.ts +38 -0
  26. package/Http/Headers.d.ts.map +1 -0
  27. package/Http/Headers.js +54 -0
  28. package/Http/Headers.js.map +1 -0
  29. package/Http/IncomingMessage.d.ts +37 -0
  30. package/Http/IncomingMessage.d.ts.map +1 -0
  31. package/Http/IncomingMessage.js +30 -0
  32. package/Http/IncomingMessage.js.map +1 -0
  33. package/Http/Method.d.ts +10 -0
  34. package/Http/Method.d.ts.map +1 -0
  35. package/Http/Method.js +12 -0
  36. package/Http/Method.js.map +1 -0
  37. package/Http/UrlParams.d.ts +56 -0
  38. package/Http/UrlParams.d.ts.map +1 -0
  39. package/Http/UrlParams.js +74 -0
  40. package/Http/UrlParams.js.map +1 -0
  41. package/HttpClient.d.ts +32 -0
  42. package/HttpClient.d.ts.map +1 -0
  43. package/HttpClient.js +23 -0
  44. package/HttpClient.js.map +1 -0
  45. package/internal/http/body.d.ts +2 -0
  46. package/internal/http/body.d.ts.map +1 -0
  47. package/internal/http/body.js +101 -0
  48. package/internal/http/body.js.map +1 -0
  49. package/internal/http/client.d.ts +3 -0
  50. package/internal/http/client.d.ts.map +1 -0
  51. package/internal/http/client.js +147 -0
  52. package/internal/http/client.js.map +1 -0
  53. package/internal/http/clientError.d.ts +2 -0
  54. package/internal/http/clientError.d.ts.map +1 -0
  55. package/internal/http/clientError.js +24 -0
  56. package/internal/http/clientError.js.map +1 -0
  57. package/internal/http/clientRequest.d.ts +2 -0
  58. package/internal/http/clientRequest.d.ts.map +1 -0
  59. package/internal/http/clientRequest.js +180 -0
  60. package/internal/http/clientRequest.js.map +1 -0
  61. package/internal/http/clientResponse.d.ts +2 -0
  62. package/internal/http/clientResponse.d.ts.map +1 -0
  63. package/internal/http/clientResponse.js +101 -0
  64. package/internal/http/clientResponse.js.map +1 -0
  65. package/mjs/Http/Body.mjs +52 -0
  66. package/mjs/Http/Body.mjs.map +1 -0
  67. package/mjs/Http/Client.mjs +97 -0
  68. package/mjs/Http/Client.mjs.map +1 -0
  69. package/mjs/Http/ClientError.mjs +17 -0
  70. package/mjs/Http/ClientError.mjs.map +1 -0
  71. package/mjs/Http/ClientRequest.mjs +162 -0
  72. package/mjs/Http/ClientRequest.mjs.map +1 -0
  73. package/mjs/Http/ClientResponse.mjs +18 -0
  74. package/mjs/Http/ClientResponse.mjs.map +1 -0
  75. package/mjs/Http/FormData.mjs +6 -0
  76. package/mjs/Http/FormData.mjs.map +1 -0
  77. package/mjs/Http/Headers.mjs +40 -0
  78. package/mjs/Http/Headers.mjs.map +1 -0
  79. package/mjs/Http/IncomingMessage.mjs +19 -0
  80. package/mjs/Http/IncomingMessage.mjs.map +1 -0
  81. package/mjs/Http/Method.mjs +5 -0
  82. package/mjs/Http/Method.mjs.map +1 -0
  83. package/mjs/Http/UrlParams.mjs +57 -0
  84. package/mjs/Http/UrlParams.mjs.map +1 -0
  85. package/mjs/HttpClient.mjs +18 -0
  86. package/mjs/HttpClient.mjs.map +1 -0
  87. package/mjs/internal/http/body.mjs +83 -0
  88. package/mjs/internal/http/body.mjs.map +1 -0
  89. package/mjs/internal/http/client.mjs +120 -0
  90. package/mjs/internal/http/client.mjs.map +1 -0
  91. package/mjs/internal/http/clientError.mjs +13 -0
  92. package/mjs/internal/http/clientError.mjs.map +1 -0
  93. package/mjs/internal/http/clientRequest.mjs +138 -0
  94. package/mjs/internal/http/clientRequest.mjs.map +1 -0
  95. package/mjs/internal/http/clientResponse.mjs +91 -0
  96. package/mjs/internal/http/clientResponse.mjs.map +1 -0
  97. package/package.json +2 -1
  98. package/src/Http/Body.ts +148 -0
  99. package/src/Http/Client.ts +280 -0
  100. package/src/Http/ClientError.ts +80 -0
  101. package/src/Http/ClientRequest.ts +320 -0
  102. package/src/Http/ClientResponse.ts +42 -0
  103. package/src/Http/FormData.ts +61 -0
  104. package/src/Http/Headers.ts +69 -0
  105. package/src/Http/IncomingMessage.ts +45 -0
  106. package/src/Http/Method.ts +17 -0
  107. package/src/Http/UrlParams.ts +106 -0
  108. package/src/HttpClient.ts +32 -0
  109. package/src/internal/http/body.ts +113 -0
  110. package/src/internal/http/client.ts +437 -0
  111. package/src/internal/http/clientError.ts +20 -0
  112. package/src/internal/http/clientRequest.ts +371 -0
  113. package/src/internal/http/clientResponse.ts +114 -0
@@ -0,0 +1,42 @@
1
+ /**
2
+ * @since 1.0.0
3
+ */
4
+ import type * as Error from "@effect/platform/Http/ClientError"
5
+ import type * as ClientRequest from "@effect/platform/Http/ClientRequest"
6
+ import type * as IncomingMessage from "@effect/platform/Http/IncomingMessage"
7
+ import * as internal from "@effect/platform/internal/http/clientResponse"
8
+
9
+ export {
10
+ /**
11
+ * @since 1.0.0
12
+ * @category schema
13
+ */
14
+ parseSchema
15
+ } from "@effect/platform/Http/IncomingMessage"
16
+
17
+ /**
18
+ * @since 1.0.0
19
+ * @category type ids
20
+ */
21
+ export const TypeId: unique symbol = internal.TypeId
22
+
23
+ /**
24
+ * @since 1.0.0
25
+ * @category type ids
26
+ */
27
+ export type TypeId = typeof TypeId
28
+
29
+ /**
30
+ * @since 1.0.0
31
+ * @category models
32
+ */
33
+ export interface ClientResponse extends IncomingMessage.IncomingMessage<Error.ResponseError> {
34
+ readonly [TypeId]: TypeId
35
+ readonly status: number
36
+ }
37
+
38
+ /**
39
+ * @since 1.0.0
40
+ * @category constructors
41
+ */
42
+ export const fromWeb: (request: ClientRequest.ClientRequest, source: Response) => ClientResponse = internal.fromWeb
@@ -0,0 +1,61 @@
1
+ /**
2
+ * @since 1.0.0
3
+ */
4
+ import type * as Error from "@effect/platform/Http/ClientError"
5
+ import type * as Stream from "@effect/stream/Stream"
6
+
7
+ /**
8
+ * @since 1.0.0
9
+ * @category type ids
10
+ */
11
+ export const TypeId = Symbol.for("@effect/platform/Http/FormData")
12
+
13
+ /**
14
+ * @since 1.0.0
15
+ * @category type ids
16
+ */
17
+ export type TypeId = typeof TypeId
18
+
19
+ /**
20
+ * @since 1.0.0
21
+ * @category models
22
+ */
23
+ export type Part = Field | File
24
+
25
+ /**
26
+ * @since 1.0.0
27
+ */
28
+ export namespace Part {
29
+ /**
30
+ * @since 1.0.0
31
+ * @category models
32
+ */
33
+ export interface Proto {
34
+ readonly [TypeId]: TypeId
35
+ readonly _tag: string
36
+ }
37
+ }
38
+
39
+ /**
40
+ * @since 1.0.0
41
+ * @category models
42
+ */
43
+ export interface Field extends Part.Proto {
44
+ readonly _tag: "FormDataField"
45
+ readonly key: string
46
+ readonly contentType: string
47
+ readonly value: string
48
+ }
49
+
50
+ /**
51
+ * @since 1.0.0
52
+ * @category models
53
+ */
54
+ export interface File extends Part.Proto {
55
+ readonly _tag: "File"
56
+ readonly key: string
57
+ readonly name: string
58
+ readonly contentType: string
59
+ readonly content: Stream.Stream<never, Error.RequestError, Uint8Array>
60
+ readonly source?: unknown
61
+ }
@@ -0,0 +1,69 @@
1
+ /**
2
+ * @since 1.0.0
3
+ */
4
+ import { dual } from "@effect/data/Function"
5
+ import * as HashMap from "@effect/data/HashMap"
6
+
7
+ /**
8
+ * @since 1.0.0
9
+ * @category models
10
+ */
11
+ export interface Headers extends HashMap.HashMap<string, string> {}
12
+
13
+ /**
14
+ * @since 1.0.0
15
+ * @category models
16
+ */
17
+ export type Input = Headers | Readonly<Record<string, string>> | Iterable<readonly [string, string]>
18
+
19
+ /**
20
+ * @since 1.0.0
21
+ * @category constructors
22
+ */
23
+ export const empty: Headers = HashMap.empty()
24
+
25
+ /**
26
+ * @since 1.0.0
27
+ * @category constructors
28
+ */
29
+ export const fromInput: (input?: Input) => Headers = (input) => {
30
+ if (input === undefined) {
31
+ return empty
32
+ } else if (HashMap.isHashMap(input)) {
33
+ return input
34
+ } else if (Symbol.iterator in input) {
35
+ return HashMap.fromIterable([...input].map(([k, v]) => [k.toLowerCase(), v])) as Headers
36
+ }
37
+ return HashMap.fromIterable(Object.entries(input).map(([k, v]) => [k.toLowerCase(), v]))
38
+ }
39
+
40
+ /**
41
+ * @since 1.0.0
42
+ * @category combinators
43
+ */
44
+ export const set = dual<
45
+ (key: string, value: string) => (self: Headers) => Headers,
46
+ (self: Headers, key: string, value: string) => Headers
47
+ >(3, (self, key, value) => HashMap.set(self, key.toLowerCase(), value))
48
+
49
+ /**
50
+ * @since 1.0.0
51
+ * @category combinators
52
+ */
53
+ export const setAll = dual<
54
+ (headers: Input) => (self: Headers) => Headers,
55
+ (self: Headers, headers: Input) => Headers
56
+ >(2, (self, headers) =>
57
+ HashMap.union(
58
+ self,
59
+ fromInput(headers)
60
+ ))
61
+
62
+ /**
63
+ * @since 1.0.0
64
+ * @category combinators
65
+ */
66
+ export const remove = dual<
67
+ (key: string) => (self: Headers) => Headers,
68
+ (self: Headers, key: string) => Headers
69
+ >(2, (self, key) => HashMap.remove(self, key.toLowerCase()))
@@ -0,0 +1,45 @@
1
+ /**
2
+ * @since 1.0.0
3
+ */
4
+ import * as Effect from "@effect/io/Effect"
5
+ import type * as Headers from "@effect/platform/Http/Headers"
6
+ import type * as ParseResult from "@effect/schema/ParseResult"
7
+ import * as Schema from "@effect/schema/Schema"
8
+ import type * as Stream from "@effect/stream/Stream"
9
+
10
+ /**
11
+ * @since 1.0.0
12
+ * @category type ids
13
+ */
14
+ export const TypeId = Symbol.for("@effect/platform/Http/IncomingMessage")
15
+
16
+ /**
17
+ * @since 1.0.0
18
+ * @category type ids
19
+ */
20
+ export type TypeId = typeof TypeId
21
+
22
+ /**
23
+ * @since 1.0.0
24
+ * @category models
25
+ */
26
+ export interface IncomingMessage<E> {
27
+ readonly [TypeId]: TypeId
28
+ readonly headers: Headers.Headers
29
+ readonly json: Effect.Effect<never, E, unknown>
30
+ readonly text: Effect.Effect<never, E, string>
31
+ readonly blob: Effect.Effect<never, E, Blob>
32
+ readonly formData: Effect.Effect<never, E, FormData>
33
+ // readonly formDataStream: Stream.Stream<never, Error.TransportError, FormData.Part>
34
+ readonly stream: Stream.Stream<never, E, Uint8Array>
35
+ }
36
+
37
+ /**
38
+ * @since 1.0.0
39
+ * @category schema
40
+ */
41
+ export const parseSchema = <I, A>(schema: Schema.Schema<I, A>) => {
42
+ const parse = Schema.parse(schema)
43
+ return <E>(self: IncomingMessage<E>): Effect.Effect<never, E | ParseResult.ParseError, A> =>
44
+ Effect.flatMap(self.json, parse)
45
+ }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * @since 1.0.0
3
+ * @category models
4
+ */
5
+ export type Method =
6
+ | "GET"
7
+ | "POST"
8
+ | "PUT"
9
+ | "DELETE"
10
+ | "PATCH"
11
+ | "HEAD"
12
+ | "OPTIONS"
13
+
14
+ /**
15
+ * @since 1.0.0
16
+ */
17
+ export const hasBody = (method: Method): boolean => method !== "GET" && method !== "HEAD"
@@ -0,0 +1,106 @@
1
+ /**
2
+ * @since 1.0.0
3
+ */
4
+ import * as Chunk from "@effect/data/Chunk"
5
+ import { dual } from "@effect/data/Function"
6
+
7
+ /**
8
+ * @since 1.0.0
9
+ * @category models
10
+ */
11
+ export interface UrlParams extends Chunk.Chunk<[string, string]> {}
12
+
13
+ /**
14
+ * @since 1.0.0
15
+ * @category models
16
+ */
17
+ export type Input = UrlParams | Readonly<Record<string, string>> | Iterable<readonly [string, string]> | URLSearchParams
18
+
19
+ /**
20
+ * @since 1.0.0
21
+ * @category constructors
22
+ */
23
+ export const fromInput = (input: Input): UrlParams => {
24
+ if (Chunk.isChunk(input)) {
25
+ return input
26
+ } else if (Symbol.iterator in input) {
27
+ return Chunk.fromIterable(input) as UrlParams
28
+ }
29
+ return Chunk.fromIterable(Object.entries(input))
30
+ }
31
+
32
+ /**
33
+ * @since 1.0.0
34
+ * @category constructors
35
+ */
36
+ export const empty: UrlParams = Chunk.empty()
37
+
38
+ /**
39
+ * @since 1.0.0
40
+ * @category combinators
41
+ */
42
+ export const set = dual<
43
+ (key: string, value: string) => (self: UrlParams) => UrlParams,
44
+ (self: UrlParams, key: string, value: string) => UrlParams
45
+ >(3, (self, key, value) =>
46
+ Chunk.append(
47
+ Chunk.filter(self, ([k]) => k !== key),
48
+ [key, value]
49
+ ))
50
+
51
+ /**
52
+ * @since 1.0.0
53
+ * @category combinators
54
+ */
55
+ export const setAll = dual<
56
+ (input: Input) => (self: UrlParams) => UrlParams,
57
+ (self: UrlParams, input: Input) => UrlParams
58
+ >(2, (self, input) => {
59
+ const toSet = fromInput(input)
60
+ const keys = Chunk.toReadonlyArray(toSet).map(([k]) => k)
61
+ return Chunk.appendAll(
62
+ Chunk.filter(self, ([k]) => keys.includes(k)),
63
+ toSet
64
+ )
65
+ })
66
+
67
+ /**
68
+ * @since 1.0.0
69
+ * @category combinators
70
+ */
71
+ export const append = dual<
72
+ (key: string, value: string) => (self: UrlParams) => UrlParams,
73
+ (self: UrlParams, key: string, value: string) => UrlParams
74
+ >(3, (self, key, value) =>
75
+ Chunk.append(
76
+ self,
77
+ [key, value]
78
+ ))
79
+
80
+ /**
81
+ * @since 1.0.0
82
+ * @category combinators
83
+ */
84
+ export const appendAll = dual<
85
+ (input: Input) => (self: UrlParams) => UrlParams,
86
+ (self: UrlParams, input: Input) => UrlParams
87
+ >(2, (self, input) =>
88
+ Chunk.appendAll(
89
+ self,
90
+ fromInput(input)
91
+ ))
92
+
93
+ /**
94
+ * @since 1.0.0
95
+ * @category combinators
96
+ */
97
+ export const remove = dual<
98
+ (key: string) => (self: UrlParams) => UrlParams,
99
+ (self: UrlParams, key: string) => UrlParams
100
+ >(2, (self, key) => Chunk.filter(self, ([k]) => k !== key))
101
+
102
+ /**
103
+ * @since 1.0.0
104
+ * @category combinators
105
+ */
106
+ export const toString = (self: UrlParams): string => new URLSearchParams(Chunk.toReadonlyArray(self) as any).toString()
@@ -0,0 +1,32 @@
1
+ /**
2
+ * @since 1.0.0
3
+ */
4
+
5
+ /**
6
+ * @since 1.0.0
7
+ */
8
+ export * as body from "@effect/platform/Http/Body"
9
+ /**
10
+ * @since 1.0.0
11
+ */
12
+ export * as client from "@effect/platform/Http/Client"
13
+ /**
14
+ * @since 1.0.0
15
+ */
16
+ export * as request from "@effect/platform/Http/ClientRequest"
17
+ /**
18
+ * @since 1.0.0
19
+ */
20
+ export * as response from "@effect/platform/Http/ClientResponse"
21
+ /**
22
+ * @since 1.0.0
23
+ */
24
+ export * as error from "@effect/platform/Http/ClientError"
25
+ /**
26
+ * @since 1.0.0
27
+ */
28
+ export * as headers from "@effect/platform/Http/Headers"
29
+ /**
30
+ * @since 1.0.0
31
+ */
32
+ export * as urlParams from "@effect/platform/Http/UrlParams"
@@ -0,0 +1,113 @@
1
+ import * as Effect from "@effect/io/Effect"
2
+ import type * as Body from "@effect/platform/Http/Body"
3
+ import * as Schema from "@effect/schema/Schema"
4
+ import type * as Stream_ from "@effect/stream/Stream"
5
+
6
+ /** @internal */
7
+ export const TypeId: Body.TypeId = Symbol.for(
8
+ "@effect/platform/Http/Body"
9
+ ) as Body.TypeId
10
+
11
+ class EmptyImpl implements Body.Empty {
12
+ readonly [TypeId]: Body.TypeId = TypeId
13
+ readonly _tag = "Empty"
14
+ }
15
+
16
+ /** @internal */
17
+ export const empty: Body.Empty = new EmptyImpl()
18
+
19
+ class RawImpl implements Body.Raw {
20
+ readonly [TypeId]: Body.TypeId = TypeId
21
+ readonly _tag = "Raw"
22
+ constructor(
23
+ readonly body: unknown,
24
+ readonly contentType?: string,
25
+ readonly contentLength?: number
26
+ ) {}
27
+ }
28
+
29
+ /** @internal */
30
+ export const raw = (body: unknown, contentType?: string, contentLength?: number): Body.Raw =>
31
+ new RawImpl(body, contentType, contentLength)
32
+
33
+ class BytesImpl implements Body.Bytes {
34
+ readonly [TypeId]: Body.TypeId = TypeId
35
+ readonly _tag = "Bytes"
36
+ constructor(
37
+ readonly body: Uint8Array,
38
+ readonly contentType?: string
39
+ ) {}
40
+ get contentLength(): number {
41
+ return this.body.length
42
+ }
43
+ }
44
+
45
+ /** @internal */
46
+ export const bytes = (body: Uint8Array, contentType?: string): Body.Bytes => new BytesImpl(body, contentType)
47
+
48
+ /** @internal */
49
+ export const text = (body: string, contentType?: string): Body.Bytes =>
50
+ bytes(new TextEncoder().encode(body), contentType)
51
+
52
+ class BytesEffectImpl implements Body.BytesEffect {
53
+ readonly [TypeId]: Body.TypeId = TypeId
54
+ readonly _tag = "BytesEffect"
55
+ constructor(
56
+ readonly body: Effect.Effect<never, unknown, Uint8Array>,
57
+ readonly contentType?: string
58
+ ) {}
59
+ }
60
+
61
+ /** @internal */
62
+ export const bytesEffect = (
63
+ body: Effect.Effect<never, unknown, Uint8Array>,
64
+ contentType?: string
65
+ ): Body.BytesEffect => new BytesEffectImpl(body, contentType)
66
+
67
+ /** @internal */
68
+ export const json = (body: unknown): Body.BytesEffect =>
69
+ bytesEffect(
70
+ Effect.try(() => new TextEncoder().encode(JSON.stringify(body))),
71
+ "application/json"
72
+ )
73
+
74
+ /** @internal */
75
+ export const jsonSchema = <I, A>(schema: Schema.Schema<I, A>) => {
76
+ const encode = Schema.encode(schema)
77
+ return (body: A): Body.BytesEffect =>
78
+ bytesEffect(
79
+ Effect.flatMap(
80
+ encode(body),
81
+ (json) => Effect.try(() => new TextEncoder().encode(JSON.stringify(json)))
82
+ ),
83
+ "application/json"
84
+ )
85
+ }
86
+
87
+ class FormDataImpl implements Body.FormData {
88
+ readonly [TypeId]: Body.TypeId = TypeId
89
+ readonly _tag = "FormData"
90
+ constructor(
91
+ readonly formData: FormData
92
+ ) {}
93
+ }
94
+
95
+ /** @internal */
96
+ export const formData = (body: FormData): Body.FormData => new FormDataImpl(body)
97
+
98
+ class StreamImpl implements Body.Stream {
99
+ readonly [TypeId]: Body.TypeId = TypeId
100
+ readonly _tag = "Stream"
101
+ constructor(
102
+ readonly stream: Stream_.Stream<never, unknown, Uint8Array>,
103
+ readonly contentType?: string,
104
+ readonly contentLength?: number
105
+ ) {}
106
+ }
107
+
108
+ /** @internal */
109
+ export const stream = (
110
+ body: Stream_.Stream<never, unknown, Uint8Array>,
111
+ contentType?: string,
112
+ contentLength?: number
113
+ ): Body.Stream => new StreamImpl(body, contentType, contentLength)