fx-fetch 1.1.1 → 1.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/dist/Fetch/Fetch.d.ts +20 -5
- package/dist/Fetch/Fetch.js +5 -4
- package/dist/Fetch/Fetch.js.map +1 -1
- package/dist/Fetch/FetchLive.d.ts +27 -2
- package/dist/Fetch/FetchLive.js +31 -42
- package/dist/Fetch/FetchLive.js.map +1 -1
- package/dist/Fetch/executeFetch.d.ts +9 -0
- package/dist/Fetch/executeFetch.js +45 -0
- package/dist/Fetch/executeFetch.js.map +1 -0
- package/dist/Fetch/fetchArrayBuffer.d.ts +1 -1
- package/dist/Fetch/fetchBlob.d.ts +1 -1
- package/dist/Fetch/fetchBytes.d.ts +1 -1
- package/dist/Fetch/fetchFn.d.ts +1 -1
- package/dist/Fetch/fetchFormData.d.ts +1 -1
- package/dist/Fetch/fetchJson.d.ts +1 -1
- package/dist/Fetch/fetchReadableStream.d.ts +1 -1
- package/dist/Fetch/fetchText.d.ts +1 -1
- package/dist/Fetch/index.d.ts +3 -0
- package/dist/Fetch/index.js +3 -0
- package/dist/Fetch/index.js.map +1 -1
- package/dist/Fetch/layer.d.ts +27 -0
- package/dist/Fetch/layer.js +29 -0
- package/dist/Fetch/layer.js.map +1 -0
- package/dist/Fetch/makeMiddlewareLayer.d.ts +70 -0
- package/dist/Fetch/makeMiddlewareLayer.js +75 -0
- package/dist/Fetch/makeMiddlewareLayer.js.map +1 -0
- package/dist/Fetch/makeMockLayer.d.ts +50 -0
- package/dist/Fetch/makeMockLayer.js +56 -0
- package/dist/Fetch/makeMockLayer.js.map +1 -0
- package/dist/Request/Request.d.ts +1 -1
- package/dist/Request/classifyRequestInput.js +6 -0
- package/dist/Request/classifyRequestInput.js.map +1 -1
- package/dist/Request/make.d.ts +9 -0
- package/dist/Request/make.js +9 -0
- package/dist/Request/make.js.map +1 -1
- package/dist/Request/unsafeMake.d.ts +9 -0
- package/dist/Request/unsafeMake.js +9 -1
- package/dist/Request/unsafeMake.js.map +1 -1
- package/package.json +5 -5
package/dist/Fetch/Fetch.d.ts
CHANGED
|
@@ -2,21 +2,36 @@ import { type Effect } from 'effect/Effect';
|
|
|
2
2
|
import type { Request } from "../Request/index.js";
|
|
3
3
|
import { NotOkError, type Response } from "../Response/index.js";
|
|
4
4
|
import { AbortError, FetchError, NotAllowedError } from "./errors.js";
|
|
5
|
-
declare
|
|
5
|
+
export declare namespace Fetch {
|
|
6
|
+
/**
|
|
7
|
+
* Type alias for the successful response type of the Fetch service.
|
|
8
|
+
* @since 1.2.0
|
|
9
|
+
* @category Models
|
|
10
|
+
*/
|
|
11
|
+
type SuccessType = Response;
|
|
12
|
+
/**
|
|
13
|
+
* Type alias for the failure response type of the Fetch service.
|
|
14
|
+
* @since 1.2.0
|
|
15
|
+
* @category Models
|
|
16
|
+
*/
|
|
17
|
+
type ErrorType = AbortError | FetchError | NotAllowedError | NotOkError;
|
|
18
|
+
}
|
|
19
|
+
declare const Fetch_base: import("effect/Context").TagClass<Fetch, "fx-fetch/Fetch", (request: Request) => Effect<Fetch.SuccessType, Fetch.ErrorType, never>>;
|
|
6
20
|
/**
|
|
7
21
|
* Fetch service for making or mocking HTTP requests.
|
|
8
22
|
*
|
|
9
23
|
* @example
|
|
10
|
-
* ```
|
|
11
|
-
* import {
|
|
24
|
+
* ```ts
|
|
25
|
+
* import { Effect } from 'effect';
|
|
26
|
+
* import { Fetch, Request } from 'fx-fetch';
|
|
12
27
|
*
|
|
13
28
|
* Effect.gen(function* () {
|
|
14
|
-
* const request =
|
|
29
|
+
* const request = Request.unsafeMake({ url: 'https://example.com' });
|
|
15
30
|
*
|
|
16
31
|
* const fetch = yield* Fetch.Fetch; // ◀︎── Get the Fetch service
|
|
17
32
|
* const response = yield* fetch(request);
|
|
18
33
|
* }).pipe(
|
|
19
|
-
* Effect.
|
|
34
|
+
* Effect.provide(Fetch.layer), // ◀︎── Provide built-in Fetch layer
|
|
20
35
|
* Effect.runPromise
|
|
21
36
|
* );
|
|
22
37
|
* ```
|
package/dist/Fetch/Fetch.js
CHANGED
|
@@ -3,16 +3,17 @@ import { Tag } from 'effect/Context';
|
|
|
3
3
|
* Fetch service for making or mocking HTTP requests.
|
|
4
4
|
*
|
|
5
5
|
* @example
|
|
6
|
-
* ```
|
|
7
|
-
* import {
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { Effect } from 'effect';
|
|
8
|
+
* import { Fetch, Request } from 'fx-fetch';
|
|
8
9
|
*
|
|
9
10
|
* Effect.gen(function* () {
|
|
10
|
-
* const request =
|
|
11
|
+
* const request = Request.unsafeMake({ url: 'https://example.com' });
|
|
11
12
|
*
|
|
12
13
|
* const fetch = yield* Fetch.Fetch; // ◀︎── Get the Fetch service
|
|
13
14
|
* const response = yield* fetch(request);
|
|
14
15
|
* }).pipe(
|
|
15
|
-
* Effect.
|
|
16
|
+
* Effect.provide(Fetch.layer), // ◀︎── Provide built-in Fetch layer
|
|
16
17
|
* Effect.runPromise
|
|
17
18
|
* );
|
|
18
19
|
* ```
|
package/dist/Fetch/Fetch.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Fetch.js","sourceRoot":"","sources":["../../src/Fetch/Fetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"Fetch.js","sourceRoot":"","sources":["../../src/Fetch/Fetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AAsBrC;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,OAAO,KAAM,SAAQ,GAAG,CAAC,gBAAgB,CAAC,EAG7C;CAAG"}
|
|
@@ -1,8 +1,33 @@
|
|
|
1
1
|
import type { Type } from "./Type.js";
|
|
2
2
|
/**
|
|
3
|
-
* Fetch
|
|
3
|
+
* Live implementation of the Fetch service that performs actual HTTP requests.
|
|
4
4
|
*
|
|
5
|
-
* @
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { Effect } from 'effect';
|
|
8
|
+
* import { Fetch, Request, Response } from 'fx-fetch';
|
|
9
|
+
*
|
|
10
|
+
* // ┌─── Effect.Effect<
|
|
11
|
+
* // │ Response.Response,
|
|
12
|
+
* // │ | Fetch.FetchError
|
|
13
|
+
* // │ | Fetch.AbortError
|
|
14
|
+
* // │ | Fetch.NotAllowedError
|
|
15
|
+
* // │ | Response.NotOkError,
|
|
16
|
+
* // │ never
|
|
17
|
+
* // │ >
|
|
18
|
+
* // ▼
|
|
19
|
+
* const program = Effect.gen(function* () {
|
|
20
|
+
* const request = Request.unsafeMake({ url: 'https://example.com' });
|
|
21
|
+
*
|
|
22
|
+
* // ┌─── Response.Response
|
|
23
|
+
* // ▼
|
|
24
|
+
* const response = yield* Fetch.FetchLive(request);
|
|
25
|
+
*
|
|
26
|
+
* return response;
|
|
27
|
+
* });
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @category Services
|
|
6
31
|
* @since 0.1.0
|
|
7
32
|
*/
|
|
8
33
|
export declare const FetchLive: Type;
|
package/dist/Fetch/FetchLive.js
CHANGED
|
@@ -1,47 +1,36 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { toJsRequestPromise } from "../Request/index.js";
|
|
5
|
-
import { ensureOk, make } from "../Response/index.js";
|
|
6
|
-
import { getErrorMessage } from "../utils/getErrorMessage.js";
|
|
7
|
-
import { AbortError, FetchError, NotAllowedError } from "./errors.js";
|
|
8
|
-
// TODO: Add examples
|
|
1
|
+
import { flatMap } from 'effect/Effect';
|
|
2
|
+
import { ensureOk } from "../Response/index.js";
|
|
3
|
+
import { executeFetch } from "./executeFetch.js";
|
|
9
4
|
/**
|
|
10
|
-
* Fetch
|
|
5
|
+
* Live implementation of the Fetch service that performs actual HTTP requests.
|
|
11
6
|
*
|
|
12
|
-
* @
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { Effect } from 'effect';
|
|
10
|
+
* import { Fetch, Request, Response } from 'fx-fetch';
|
|
11
|
+
*
|
|
12
|
+
* // ┌─── Effect.Effect<
|
|
13
|
+
* // │ Response.Response,
|
|
14
|
+
* // │ | Fetch.FetchError
|
|
15
|
+
* // │ | Fetch.AbortError
|
|
16
|
+
* // │ | Fetch.NotAllowedError
|
|
17
|
+
* // │ | Response.NotOkError,
|
|
18
|
+
* // │ never
|
|
19
|
+
* // │ >
|
|
20
|
+
* // ▼
|
|
21
|
+
* const program = Effect.gen(function* () {
|
|
22
|
+
* const request = Request.unsafeMake({ url: 'https://example.com' });
|
|
23
|
+
*
|
|
24
|
+
* // ┌─── Response.Response
|
|
25
|
+
* // ▼
|
|
26
|
+
* const response = yield* Fetch.FetchLive(request);
|
|
27
|
+
*
|
|
28
|
+
* return response;
|
|
29
|
+
* });
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @category Services
|
|
13
33
|
* @since 0.1.0
|
|
14
34
|
*/
|
|
15
|
-
export const FetchLive = (request) =>
|
|
16
|
-
try: async (signal) => {
|
|
17
|
-
const jsRequest = await toJsRequestPromise(request, { signal });
|
|
18
|
-
return await globalThis.fetch(jsRequest);
|
|
19
|
-
},
|
|
20
|
-
catch(error) {
|
|
21
|
-
if (error instanceof TypeError) {
|
|
22
|
-
return new FetchError({
|
|
23
|
-
message: error.message,
|
|
24
|
-
cause: error,
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
if (typeof error === 'object' && error !== null && 'name' in error) {
|
|
28
|
-
if (error.name === 'AbortError') {
|
|
29
|
-
return new AbortError({
|
|
30
|
-
message: getErrorMessage(error, 'Http request aborted'),
|
|
31
|
-
cause: error,
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
if (error.name === 'NotAllowedError') {
|
|
35
|
-
return new NotAllowedError({
|
|
36
|
-
message: getErrorMessage(error, 'Http request not allowed'),
|
|
37
|
-
cause: error,
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
throw new RuntimeException(getErrorMessage(error, 'Unknown error occurred during fetch request'));
|
|
42
|
-
},
|
|
43
|
-
}).pipe(map(make), flatMap(match({
|
|
44
|
-
onSome: succeed,
|
|
45
|
-
onNone: () => dieMessage('Failed to create Response from fetch response'),
|
|
46
|
-
})), flatMap(ensureOk));
|
|
35
|
+
export const FetchLive = (request) => executeFetch(request).pipe(flatMap(ensureOk));
|
|
47
36
|
//# sourceMappingURL=FetchLive.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FetchLive.js","sourceRoot":"","sources":["../../src/Fetch/FetchLive.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"FetchLive.js","sourceRoot":"","sources":["../../src/Fetch/FetchLive.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAExC,OAAO,EAAE,QAAQ,EAAE,6BAAoB;AACvC,OAAO,EAAE,YAAY,EAAE,0BAAuB;AAG9C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,MAAM,CAAC,MAAM,SAAS,GAAS,CAAC,OAAgB,EAAE,EAAE,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type Request } from "../Request/index.js";
|
|
2
|
+
import { type Response } from "../Response/index.js";
|
|
3
|
+
import { AbortError, FetchError, NotAllowedError } from "./errors.js";
|
|
4
|
+
/**
|
|
5
|
+
* Core implementation of fetch execution.
|
|
6
|
+
*
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
export declare const executeFetch: (request: Request) => import("effect/Effect").Effect<Response, FetchError | AbortError | NotAllowedError, never>;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { RuntimeException } from 'effect/Cause';
|
|
2
|
+
import { dieMessage, ensureErrorType, ensureRequirementsType, ensureSuccessType, flatMap, map, succeed, tryPromise, } from 'effect/Effect';
|
|
3
|
+
import { match } from 'effect/Option';
|
|
4
|
+
import { toJsRequestPromise } from "../Request/index.js";
|
|
5
|
+
import { make as makeResponse } from "../Response/index.js";
|
|
6
|
+
import { getErrorMessage } from "../utils/getErrorMessage.js";
|
|
7
|
+
import { AbortError, FetchError, NotAllowedError } from "./errors.js";
|
|
8
|
+
/**
|
|
9
|
+
* Core implementation of fetch execution.
|
|
10
|
+
*
|
|
11
|
+
* @internal
|
|
12
|
+
*/
|
|
13
|
+
export const executeFetch = (request) => tryPromise({
|
|
14
|
+
try: async (signal) => {
|
|
15
|
+
const jsRequest = await toJsRequestPromise(request, { signal });
|
|
16
|
+
return await globalThis.fetch(jsRequest);
|
|
17
|
+
},
|
|
18
|
+
catch(error) {
|
|
19
|
+
if (error instanceof TypeError) {
|
|
20
|
+
return new FetchError({
|
|
21
|
+
message: error.message,
|
|
22
|
+
cause: error,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
if (typeof error === 'object' && error !== null && 'name' in error) {
|
|
26
|
+
if (error.name === 'AbortError') {
|
|
27
|
+
return new AbortError({
|
|
28
|
+
message: getErrorMessage(error, 'Http request aborted'),
|
|
29
|
+
cause: error,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
if (error.name === 'NotAllowedError') {
|
|
33
|
+
return new NotAllowedError({
|
|
34
|
+
message: getErrorMessage(error, 'Http request not allowed'),
|
|
35
|
+
cause: error,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
throw new RuntimeException(getErrorMessage(error, 'Unknown error occurred during fetch request'));
|
|
40
|
+
},
|
|
41
|
+
}).pipe(map(makeResponse), flatMap(match({
|
|
42
|
+
onSome: succeed,
|
|
43
|
+
onNone: () => dieMessage('Failed to create Response from fetch response'),
|
|
44
|
+
})), ensureSuccessType(), ensureErrorType(), ensureRequirementsType());
|
|
45
|
+
//# sourceMappingURL=executeFetch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"executeFetch.js","sourceRoot":"","sources":["../../src/Fetch/executeFetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EACL,UAAU,EACV,eAAe,EACf,sBAAsB,EACtB,iBAAiB,EACjB,OAAO,EACP,GAAG,EACH,OAAO,EACP,UAAU,GACX,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,EAAgB,kBAAkB,EAAE,4BAAmB;AAC9D,OAAO,EAAE,IAAI,IAAI,YAAY,EAAiB,6BAAoB;AAClE,OAAO,EAAE,eAAe,EAAE,oCAAiC;AAC3D,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,eAAe,EAAE,oBAAiB;AAEnE;;;;GAIG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,OAAgB,EAAE,EAAE,CAC/C,UAAU,CAAC;IACT,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE;QACpB,MAAM,SAAS,GAAG,MAAM,kBAAkB,CAAC,OAAO,EAAE,EAAE,MAAM,EAAE,CAAC,CAAC;QAChE,OAAO,MAAM,UAAU,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;IAC3C,CAAC;IACD,KAAK,CAAC,KAAK;QACT,IAAI,KAAK,YAAY,SAAS,EAAE,CAAC;YAC/B,OAAO,IAAI,UAAU,CAAC;gBACpB,OAAO,EAAE,KAAK,CAAC,OAAO;gBACtB,KAAK,EAAE,KAAK;aACb,CAAC,CAAC;QACL,CAAC;QAED,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,MAAM,IAAI,KAAK,EAAE,CAAC;YACnE,IAAI,KAAK,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;gBAChC,OAAO,IAAI,UAAU,CAAC;oBACpB,OAAO,EAAE,eAAe,CAAC,KAAK,EAAE,sBAAsB,CAAC;oBACvD,KAAK,EAAE,KAAK;iBACb,CAAC,CAAC;YACL,CAAC;YAED,IAAI,KAAK,CAAC,IAAI,KAAK,iBAAiB,EAAE,CAAC;gBACrC,OAAO,IAAI,eAAe,CAAC;oBACzB,OAAO,EAAE,eAAe,CAAC,KAAK,EAAE,0BAA0B,CAAC;oBAC3D,KAAK,EAAE,KAAK;iBACb,CAAC,CAAC;YACL,CAAC;QACH,CAAC;QAED,MAAM,IAAI,gBAAgB,CACxB,eAAe,CAAC,KAAK,EAAE,6CAA6C,CAAC,CACtE,CAAC;IACJ,CAAC;CACF,CAAC,CAAC,IAAI,CACL,GAAG,CAAC,YAAY,CAAC,EACjB,OAAO,CACL,KAAK,CAAC;IACJ,MAAM,EAAE,OAAO;IACf,MAAM,EAAE,GAAG,EAAE,CAAC,UAAU,CAAC,+CAA+C,CAAC;CAC1E,CAAC,CACH,EACD,iBAAiB,EAAY,EAC7B,eAAe,EAA6C,EAC5D,sBAAsB,EAAS,CAChC,CAAC"}
|
|
@@ -5,4 +5,4 @@ import type { Request } from "../Request/index.js";
|
|
|
5
5
|
* @category Functions
|
|
6
6
|
* @since 0.1.0
|
|
7
7
|
*/
|
|
8
|
-
export declare const fetchArrayBuffer: (request: Request) => import("effect/Effect").Effect<ArrayBuffer, import("../Cause").MalformedArrayBufferError | import("./
|
|
8
|
+
export declare const fetchArrayBuffer: (request: Request) => import("effect/Effect").Effect<ArrayBuffer, import("../Cause").MalformedArrayBufferError | import("./Fetch").Fetch.ErrorType, import("./Fetch").Fetch>;
|
|
@@ -29,4 +29,4 @@ import type { Request } from "../Request/index.js";
|
|
|
29
29
|
* });
|
|
30
30
|
* ```
|
|
31
31
|
*/
|
|
32
|
-
export declare const fetchBlob: (request: Request) => import("effect/Effect").Effect<Blob, import("../Cause").MalformedBlobError | import("./
|
|
32
|
+
export declare const fetchBlob: (request: Request) => import("effect/Effect").Effect<Blob, import("../Cause").MalformedBlobError | import("./Fetch").Fetch.ErrorType, import("./Fetch").Fetch>;
|
|
@@ -29,4 +29,4 @@ import type { Request } from "../Request/index.js";
|
|
|
29
29
|
* });
|
|
30
30
|
* ```
|
|
31
31
|
*/
|
|
32
|
-
export declare const fetchBytes: (request: Request) => import("effect/Effect").Effect<Uint8Array<ArrayBufferLike>, import("../Cause").MalformedBytesError | import("./
|
|
32
|
+
export declare const fetchBytes: (request: Request) => import("effect/Effect").Effect<Uint8Array<ArrayBufferLike>, import("../Cause").MalformedBytesError | import("./Fetch").Fetch.ErrorType, import("./Fetch").Fetch>;
|
package/dist/Fetch/fetchFn.d.ts
CHANGED
|
@@ -32,4 +32,4 @@ import { Fetch } from "./Fetch.js";
|
|
|
32
32
|
* );
|
|
33
33
|
* ```
|
|
34
34
|
*/
|
|
35
|
-
export declare const fetch: (request: Request) => import("effect/Effect").Effect<import("../Response").Response,
|
|
35
|
+
export declare const fetch: (request: Request) => import("effect/Effect").Effect<import("../Response").Response, Fetch.ErrorType, Fetch>;
|
|
@@ -29,4 +29,4 @@ import type { Request } from "../Request/index.js";
|
|
|
29
29
|
* });
|
|
30
30
|
* ```
|
|
31
31
|
*/
|
|
32
|
-
export declare const fetchFormData: (request: Request) => import("effect/Effect").Effect<FormData, import("../Cause").MalformedFormDataError | import("./
|
|
32
|
+
export declare const fetchFormData: (request: Request) => import("effect/Effect").Effect<FormData, import("../Cause").MalformedFormDataError | import("./Fetch").Fetch.ErrorType, import("./Fetch").Fetch>;
|
|
@@ -29,4 +29,4 @@ import type { Request } from "../Request/index.js";
|
|
|
29
29
|
* });
|
|
30
30
|
* ```
|
|
31
31
|
*/
|
|
32
|
-
export declare const fetchJson: (request: Request) => import("effect/Effect").Effect<unknown, import("../Cause").MalformedJsonError | import("./
|
|
32
|
+
export declare const fetchJson: (request: Request) => import("effect/Effect").Effect<unknown, import("../Cause").MalformedJsonError | import("./Fetch").Fetch.ErrorType, import("./Fetch").Fetch>;
|
|
@@ -29,4 +29,4 @@ import type { Request } from "../Request/index.js";
|
|
|
29
29
|
* });
|
|
30
30
|
* ```
|
|
31
31
|
*/
|
|
32
|
-
export declare const fetchReadableStream: (request: Request) => import("effect/Effect").Effect<import("../utils/localThis").ReadableStream<Uint8Array<ArrayBufferLike>>, import("../Cause").MalformedReadableStreamError | import("./
|
|
32
|
+
export declare const fetchReadableStream: (request: Request) => import("effect/Effect").Effect<import("../utils/localThis").ReadableStream<Uint8Array<ArrayBufferLike>>, import("../Cause").MalformedReadableStreamError | import("./Fetch").Fetch.ErrorType, import("./Fetch").Fetch>;
|
|
@@ -29,4 +29,4 @@ import type { Request } from "../Request/index.js";
|
|
|
29
29
|
* });
|
|
30
30
|
* ```
|
|
31
31
|
*/
|
|
32
|
-
export declare const fetchText: (request: Request) => import("effect/Effect").Effect<string, import("../Cause").MalformedTextError | import("./
|
|
32
|
+
export declare const fetchText: (request: Request) => import("effect/Effect").Effect<string, import("../Cause").MalformedTextError | import("./Fetch").Fetch.ErrorType, import("./Fetch").Fetch>;
|
package/dist/Fetch/index.d.ts
CHANGED
|
@@ -11,6 +11,9 @@ export { fetchJsonWithSchema } from "./fetchJsonWithSchema.js";
|
|
|
11
11
|
export { fetchReadableStream } from "./fetchReadableStream.js";
|
|
12
12
|
export { fetchStream } from "./fetchStream.js";
|
|
13
13
|
export { fetchText } from "./fetchText.js";
|
|
14
|
+
export { layer } from "./layer.js";
|
|
15
|
+
export { makeMiddlewareLayer } from "./makeMiddlewareLayer.js";
|
|
16
|
+
export { makeMockLayer } from "./makeMockLayer.js";
|
|
14
17
|
export { paginatedFetch } from "./paginatedFetch.js";
|
|
15
18
|
export { paginatedFetchStream } from "./paginatedFetchStream.js";
|
|
16
19
|
export type { Type } from "./Type.js";
|
package/dist/Fetch/index.js
CHANGED
|
@@ -11,6 +11,9 @@ export { fetchJsonWithSchema } from "./fetchJsonWithSchema.js";
|
|
|
11
11
|
export { fetchReadableStream } from "./fetchReadableStream.js";
|
|
12
12
|
export { fetchStream } from "./fetchStream.js";
|
|
13
13
|
export { fetchText } from "./fetchText.js";
|
|
14
|
+
export { layer } from "./layer.js";
|
|
15
|
+
export { makeMiddlewareLayer } from "./makeMiddlewareLayer.js";
|
|
16
|
+
export { makeMockLayer } from "./makeMockLayer.js";
|
|
14
17
|
export { paginatedFetch } from "./paginatedFetch.js";
|
|
15
18
|
export { paginatedFetchStream } from "./paginatedFetchStream.js";
|
|
16
19
|
//# sourceMappingURL=index.js.map
|
package/dist/Fetch/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/Fetch/index.ts"],"names":[],"mappings":"AAAA,4BAAyB;AACzB,OAAO,EAAE,KAAK,EAAE,mBAAgB;AAChC,OAAO,EAAE,SAAS,EAAE,uBAAoB;AACxC,OAAO,EAAE,gBAAgB,EAAE,8BAA2B;AACtD,OAAO,EAAE,SAAS,EAAE,uBAAoB;AACxC,OAAO,EAAE,UAAU,EAAE,wBAAqB;AAC1C,OAAO,EAAE,KAAK,EAAE,qBAAkB;AAClC,OAAO,EAAE,aAAa,EAAE,2BAAwB;AAChD,OAAO,EAAE,SAAS,EAAE,uBAAoB;AACxC,OAAO,EAAE,mBAAmB,EAAE,iCAA8B;AAC5D,OAAO,EAAE,mBAAmB,EAAE,iCAA8B;AAC5D,OAAO,EAAE,WAAW,EAAE,yBAAsB;AAC5C,OAAO,EAAE,SAAS,EAAE,uBAAoB;AACxC,OAAO,EAAE,cAAc,EAAE,4BAAyB;AAClD,OAAO,EAAE,oBAAoB,EAAE,kCAA+B"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/Fetch/index.ts"],"names":[],"mappings":"AAAA,4BAAyB;AACzB,OAAO,EAAE,KAAK,EAAE,mBAAgB;AAChC,OAAO,EAAE,SAAS,EAAE,uBAAoB;AACxC,OAAO,EAAE,gBAAgB,EAAE,8BAA2B;AACtD,OAAO,EAAE,SAAS,EAAE,uBAAoB;AACxC,OAAO,EAAE,UAAU,EAAE,wBAAqB;AAC1C,OAAO,EAAE,KAAK,EAAE,qBAAkB;AAClC,OAAO,EAAE,aAAa,EAAE,2BAAwB;AAChD,OAAO,EAAE,SAAS,EAAE,uBAAoB;AACxC,OAAO,EAAE,mBAAmB,EAAE,iCAA8B;AAC5D,OAAO,EAAE,mBAAmB,EAAE,iCAA8B;AAC5D,OAAO,EAAE,WAAW,EAAE,yBAAsB;AAC5C,OAAO,EAAE,SAAS,EAAE,uBAAoB;AACxC,OAAO,EAAE,KAAK,EAAE,mBAAgB;AAChC,OAAO,EAAE,mBAAmB,EAAE,iCAA8B;AAC5D,OAAO,EAAE,aAAa,EAAE,2BAAwB;AAChD,OAAO,EAAE,cAAc,EAAE,4BAAyB;AAClD,OAAO,EAAE,oBAAoB,EAAE,kCAA+B"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type Layer } from 'effect/Layer';
|
|
2
|
+
import { Fetch } from "./Fetch.js";
|
|
3
|
+
/**
|
|
4
|
+
* Fetch layer providing the live implementation.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { Effect } from 'effect';
|
|
9
|
+
* import { Fetch, Request } from 'fx-fetch';
|
|
10
|
+
*
|
|
11
|
+
* const program = Effect.gen(function* () {
|
|
12
|
+
* const request = Request.unsafeMake({ url: 'https://example.com' });
|
|
13
|
+
* const response = yield* Fetch.fetch(request);
|
|
14
|
+
*
|
|
15
|
+
* return response;
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* program.pipe(
|
|
19
|
+
* Effect.provide(Fetch.layer), // ◀︎── Provide Fetch layer
|
|
20
|
+
* Effect.runPromise
|
|
21
|
+
* );
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @category Layers
|
|
25
|
+
* @since 1.2.0
|
|
26
|
+
*/
|
|
27
|
+
export declare const layer: Layer<Fetch, never, never>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { succeed as layerSucceed } from 'effect/Layer';
|
|
2
|
+
import { Fetch } from "./Fetch.js";
|
|
3
|
+
import { FetchLive } from "./FetchLive.js";
|
|
4
|
+
/**
|
|
5
|
+
* Fetch layer providing the live implementation.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { Effect } from 'effect';
|
|
10
|
+
* import { Fetch, Request } from 'fx-fetch';
|
|
11
|
+
*
|
|
12
|
+
* const program = Effect.gen(function* () {
|
|
13
|
+
* const request = Request.unsafeMake({ url: 'https://example.com' });
|
|
14
|
+
* const response = yield* Fetch.fetch(request);
|
|
15
|
+
*
|
|
16
|
+
* return response;
|
|
17
|
+
* });
|
|
18
|
+
*
|
|
19
|
+
* program.pipe(
|
|
20
|
+
* Effect.provide(Fetch.layer), // ◀︎── Provide Fetch layer
|
|
21
|
+
* Effect.runPromise
|
|
22
|
+
* );
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @category Layers
|
|
26
|
+
* @since 1.2.0
|
|
27
|
+
*/
|
|
28
|
+
export const layer = layerSucceed(Fetch, FetchLive);
|
|
29
|
+
//# sourceMappingURL=layer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"layer.js","sourceRoot":"","sources":["../../src/Fetch/layer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,OAAO,IAAI,YAAY,EAAE,MAAM,cAAc,CAAC;AACnE,OAAO,EAAE,KAAK,EAAE,mBAAgB;AAChC,OAAO,EAAE,SAAS,EAAE,uBAAoB;AAExC;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,CAAC,MAAM,KAAK,GAA+B,YAAY,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { type Effect } from 'effect/Effect';
|
|
2
|
+
import { type Layer } from 'effect/Layer';
|
|
3
|
+
import type { Request } from "../Request/index.js";
|
|
4
|
+
import { type Response } from "../Response/index.js";
|
|
5
|
+
import { Fetch } from "./Fetch.js";
|
|
6
|
+
type MiddlewareOptions<R1, R2> = {
|
|
7
|
+
readonly mapRequest?: (request: Request) => Effect<Request, never, R1>;
|
|
8
|
+
readonly mapResponse?: (response: Response) => Effect<Response, Fetch.ErrorType, R2>;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Creates a middleware Layer that allows customizing request and response handling.
|
|
12
|
+
*
|
|
13
|
+
* The middleware can transform requests before they are sent and responses after they are received.
|
|
14
|
+
* Both `mapRequest` and `mapResponse` are optional - if not provided, they pass through unchanged.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { Effect } from 'effect';
|
|
19
|
+
* import { Fetch, Request } from 'fx-fetch';
|
|
20
|
+
*
|
|
21
|
+
* // Middleware that adds authorization header to all requests
|
|
22
|
+
* const authMiddlewareLayer = Fetch.makeMiddlewareLayer({
|
|
23
|
+
* mapRequest: (request) =>
|
|
24
|
+
* Effect.succeed(
|
|
25
|
+
* Request.appendHeaders(request, { Authorization: 'Bearer token' })
|
|
26
|
+
* ),
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* Effect.gen(function* () {
|
|
30
|
+
* const request = Request.unsafeMake({ url: 'https://api.example.com/data' });
|
|
31
|
+
* const response = yield* Fetch.fetch(request);
|
|
32
|
+
* }).pipe(
|
|
33
|
+
* Effect.provide(authMiddlewareLayer), // ◀︎── Provide middleware layer
|
|
34
|
+
* Effect.runPromise
|
|
35
|
+
* );
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* import { Context, Effect, Layer } from 'effect';
|
|
41
|
+
* import { Fetch, Request } from 'fx-fetch';
|
|
42
|
+
*
|
|
43
|
+
* // Service for configuration
|
|
44
|
+
* class Config extends Context.Tag('Config')<Config, { apiKey: string }>() {}
|
|
45
|
+
*
|
|
46
|
+
* // Middleware with requirements - the Layer properly tracks Config requirement
|
|
47
|
+
* const authMiddlewareLayer = Fetch.makeMiddlewareLayer({
|
|
48
|
+
* mapRequest: (request) =>
|
|
49
|
+
* Effect.gen(function* () {
|
|
50
|
+
* const config = yield* Config;
|
|
51
|
+
* return Request.appendHeaders(request, { 'X-API-Key': config.apiKey });
|
|
52
|
+
* }),
|
|
53
|
+
* });
|
|
54
|
+
* // Type: Layer<Fetch, never, Config>
|
|
55
|
+
*
|
|
56
|
+
* Effect.gen(function* () {
|
|
57
|
+
* const request = Request.unsafeMake({ url: 'https://api.example.com/data' });
|
|
58
|
+
* const response = yield* Fetch.fetch(request);
|
|
59
|
+
* }).pipe(
|
|
60
|
+
* Effect.provide(authMiddlewareLayer),
|
|
61
|
+
* Effect.provide(Layer.succeed(Config, { apiKey: 'secret' })),
|
|
62
|
+
* Effect.runPromise
|
|
63
|
+
* );
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* @category Layers
|
|
67
|
+
* @since 1.2.0
|
|
68
|
+
*/
|
|
69
|
+
export declare const makeMiddlewareLayer: <R1 = never, R2 = never>(options: MiddlewareOptions<R1, R2>) => Layer<Fetch, never, R1 | R2>;
|
|
70
|
+
export {};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { context, flatMap, gen, provide, succeed } from 'effect/Effect';
|
|
2
|
+
import { effect as layerEffect } from 'effect/Layer';
|
|
3
|
+
import { ensureOk } from "../Response/index.js";
|
|
4
|
+
import { executeFetch } from "./executeFetch.js";
|
|
5
|
+
import { Fetch } from "./Fetch.js";
|
|
6
|
+
/**
|
|
7
|
+
* Creates a middleware Layer that allows customizing request and response handling.
|
|
8
|
+
*
|
|
9
|
+
* The middleware can transform requests before they are sent and responses after they are received.
|
|
10
|
+
* Both `mapRequest` and `mapResponse` are optional - if not provided, they pass through unchanged.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { Effect } from 'effect';
|
|
15
|
+
* import { Fetch, Request } from 'fx-fetch';
|
|
16
|
+
*
|
|
17
|
+
* // Middleware that adds authorization header to all requests
|
|
18
|
+
* const authMiddlewareLayer = Fetch.makeMiddlewareLayer({
|
|
19
|
+
* mapRequest: (request) =>
|
|
20
|
+
* Effect.succeed(
|
|
21
|
+
* Request.appendHeaders(request, { Authorization: 'Bearer token' })
|
|
22
|
+
* ),
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* Effect.gen(function* () {
|
|
26
|
+
* const request = Request.unsafeMake({ url: 'https://api.example.com/data' });
|
|
27
|
+
* const response = yield* Fetch.fetch(request);
|
|
28
|
+
* }).pipe(
|
|
29
|
+
* Effect.provide(authMiddlewareLayer), // ◀︎── Provide middleware layer
|
|
30
|
+
* Effect.runPromise
|
|
31
|
+
* );
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* import { Context, Effect, Layer } from 'effect';
|
|
37
|
+
* import { Fetch, Request } from 'fx-fetch';
|
|
38
|
+
*
|
|
39
|
+
* // Service for configuration
|
|
40
|
+
* class Config extends Context.Tag('Config')<Config, { apiKey: string }>() {}
|
|
41
|
+
*
|
|
42
|
+
* // Middleware with requirements - the Layer properly tracks Config requirement
|
|
43
|
+
* const authMiddlewareLayer = Fetch.makeMiddlewareLayer({
|
|
44
|
+
* mapRequest: (request) =>
|
|
45
|
+
* Effect.gen(function* () {
|
|
46
|
+
* const config = yield* Config;
|
|
47
|
+
* return Request.appendHeaders(request, { 'X-API-Key': config.apiKey });
|
|
48
|
+
* }),
|
|
49
|
+
* });
|
|
50
|
+
* // Type: Layer<Fetch, never, Config>
|
|
51
|
+
*
|
|
52
|
+
* Effect.gen(function* () {
|
|
53
|
+
* const request = Request.unsafeMake({ url: 'https://api.example.com/data' });
|
|
54
|
+
* const response = yield* Fetch.fetch(request);
|
|
55
|
+
* }).pipe(
|
|
56
|
+
* Effect.provide(authMiddlewareLayer),
|
|
57
|
+
* Effect.provide(Layer.succeed(Config, { apiKey: 'secret' })),
|
|
58
|
+
* Effect.runPromise
|
|
59
|
+
* );
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @category Layers
|
|
63
|
+
* @since 1.2.0
|
|
64
|
+
*/
|
|
65
|
+
export const makeMiddlewareLayer = (options) => {
|
|
66
|
+
const mapRequest = options.mapRequest ?? ((request) => succeed(request));
|
|
67
|
+
const mapResponse = options.mapResponse ?? ((response) => succeed(response));
|
|
68
|
+
const makeService = gen(function* () {
|
|
69
|
+
const ctx = yield* context();
|
|
70
|
+
const service = (request) => mapRequest(request).pipe(flatMap(executeFetch), flatMap(mapResponse), flatMap(ensureOk), provide(ctx));
|
|
71
|
+
return service;
|
|
72
|
+
});
|
|
73
|
+
return layerEffect(Fetch, makeService);
|
|
74
|
+
};
|
|
75
|
+
//# sourceMappingURL=makeMiddlewareLayer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"makeMiddlewareLayer.js","sourceRoot":"","sources":["../../src/Fetch/makeMiddlewareLayer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAe,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACrF,OAAO,EAAc,MAAM,IAAI,WAAW,EAAE,MAAM,cAAc,CAAC;AAEjE,OAAO,EAAE,QAAQ,EAAiB,6BAAoB;AACtD,OAAO,EAAE,YAAY,EAAE,0BAAuB;AAC9C,OAAO,EAAE,KAAK,EAAE,mBAAgB;AAQhC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0DG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CACjC,OAAkC,EACJ,EAAE;IAChC,MAAM,UAAU,GAAG,OAAO,CAAC,UAAU,IAAI,CAAC,CAAC,OAAgB,EAAE,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC;IAClF,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,IAAI,CAAC,CAAC,QAAkB,EAAE,EAAE,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC;IAEvF,MAAM,WAAW,GAAG,GAAG,CAAC,QAAQ,CAAC;QAC/B,MAAM,GAAG,GAAG,KAAK,CAAC,CAAC,OAAO,EAAW,CAAC;QAEtC,MAAM,OAAO,GAAS,CAAC,OAAgB,EAAE,EAAE,CACzC,UAAU,CAAC,OAAO,CAAC,CAAC,IAAI,CACtB,OAAO,CAAC,YAAY,CAAC,EACrB,OAAO,CAAC,WAAW,CAAC,EACpB,OAAO,CAAC,QAAQ,CAAC,EACjB,OAAO,CAAC,GAAG,CAAC,CACb,CAAC;QAEJ,OAAO,OAAO,CAAC;IACjB,CAAC,CAAC,CAAC;IAEH,OAAO,WAAW,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC;AACzC,CAAC,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { type Effect } from 'effect/Effect';
|
|
2
|
+
import { type Layer } from 'effect/Layer';
|
|
3
|
+
import type { Request } from "../Request/index.js";
|
|
4
|
+
import { Fetch } from "./Fetch.js";
|
|
5
|
+
type MockFn = (request: Request) => Fetch.SuccessType | Effect<Fetch.SuccessType, Fetch.ErrorType, never>;
|
|
6
|
+
/**
|
|
7
|
+
* Creates a mock Fetch layer for testing purposes.
|
|
8
|
+
*
|
|
9
|
+
* The mock function can return either a plain `Response` or an `Effect<Response, E>`
|
|
10
|
+
* for simulating both successful responses and error scenarios.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { Effect } from 'effect';
|
|
15
|
+
* import { Fetch, Request, Response } from 'fx-fetch';
|
|
16
|
+
*
|
|
17
|
+
* // Mock returning a simple Response
|
|
18
|
+
* const mockLayer = Fetch.makeMockLayer(() =>
|
|
19
|
+
* Response.unsafeMake({ status: 200, ok: true, body: 'Hello' })
|
|
20
|
+
* );
|
|
21
|
+
*
|
|
22
|
+
* Effect.gen(function* () {
|
|
23
|
+
* const request = Request.unsafeMake({ url: 'https://example.com' });
|
|
24
|
+
* const response = yield* Fetch.fetch(request);
|
|
25
|
+
* }).pipe(
|
|
26
|
+
* Effect.provide(mockLayer), // ◀︎── Provide mock layer
|
|
27
|
+
* Effect.runPromise
|
|
28
|
+
* );
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* import { Effect } from 'effect';
|
|
34
|
+
* import { Fetch, Request, Response, Url } from 'fx-fetch';
|
|
35
|
+
*
|
|
36
|
+
* // Mock using the request to return different responses
|
|
37
|
+
* const mockLayer = Fetch.makeMockLayer((request) => {
|
|
38
|
+
* if (Url.format(request.url).includes('/users')) {
|
|
39
|
+
* return Response.unsafeMake({ status: 200, ok: true, body: JSON.stringify([{ id: 1 }]) });
|
|
40
|
+
* }
|
|
41
|
+
*
|
|
42
|
+
* return Response.unsafeMake({ status: 404, ok: false });
|
|
43
|
+
* });
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @category Layers
|
|
47
|
+
* @since 1.2.0
|
|
48
|
+
*/
|
|
49
|
+
export declare const makeMockLayer: (mockFn: MockFn) => Layer<Fetch, never, never>;
|
|
50
|
+
export {};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { flatMap, isEffect, succeed } from 'effect/Effect';
|
|
2
|
+
import { succeed as layerSucceed } from 'effect/Layer';
|
|
3
|
+
import { ensureOk } from "../Response/index.js";
|
|
4
|
+
import { Fetch } from "./Fetch.js";
|
|
5
|
+
/**
|
|
6
|
+
* Creates a mock Fetch layer for testing purposes.
|
|
7
|
+
*
|
|
8
|
+
* The mock function can return either a plain `Response` or an `Effect<Response, E>`
|
|
9
|
+
* for simulating both successful responses and error scenarios.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { Effect } from 'effect';
|
|
14
|
+
* import { Fetch, Request, Response } from 'fx-fetch';
|
|
15
|
+
*
|
|
16
|
+
* // Mock returning a simple Response
|
|
17
|
+
* const mockLayer = Fetch.makeMockLayer(() =>
|
|
18
|
+
* Response.unsafeMake({ status: 200, ok: true, body: 'Hello' })
|
|
19
|
+
* );
|
|
20
|
+
*
|
|
21
|
+
* Effect.gen(function* () {
|
|
22
|
+
* const request = Request.unsafeMake({ url: 'https://example.com' });
|
|
23
|
+
* const response = yield* Fetch.fetch(request);
|
|
24
|
+
* }).pipe(
|
|
25
|
+
* Effect.provide(mockLayer), // ◀︎── Provide mock layer
|
|
26
|
+
* Effect.runPromise
|
|
27
|
+
* );
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* import { Effect } from 'effect';
|
|
33
|
+
* import { Fetch, Request, Response, Url } from 'fx-fetch';
|
|
34
|
+
*
|
|
35
|
+
* // Mock using the request to return different responses
|
|
36
|
+
* const mockLayer = Fetch.makeMockLayer((request) => {
|
|
37
|
+
* if (Url.format(request.url).includes('/users')) {
|
|
38
|
+
* return Response.unsafeMake({ status: 200, ok: true, body: JSON.stringify([{ id: 1 }]) });
|
|
39
|
+
* }
|
|
40
|
+
*
|
|
41
|
+
* return Response.unsafeMake({ status: 404, ok: false });
|
|
42
|
+
* });
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* @category Layers
|
|
46
|
+
* @since 1.2.0
|
|
47
|
+
*/
|
|
48
|
+
export const makeMockLayer = (mockFn) => {
|
|
49
|
+
const service = (request) => {
|
|
50
|
+
const result = mockFn(request);
|
|
51
|
+
const effect = isEffect(result) ? result : succeed(result);
|
|
52
|
+
return effect.pipe(flatMap(ensureOk));
|
|
53
|
+
};
|
|
54
|
+
return layerSucceed(Fetch, service);
|
|
55
|
+
};
|
|
56
|
+
//# sourceMappingURL=makeMockLayer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"makeMockLayer.js","sourceRoot":"","sources":["../../src/Fetch/makeMockLayer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxE,OAAO,EAAc,OAAO,IAAI,YAAY,EAAE,MAAM,cAAc,CAAC;AAEnE,OAAO,EAAE,QAAQ,EAAE,6BAAoB;AACvC,OAAO,EAAE,KAAK,EAAE,mBAAgB;AAOhC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,MAAc,EAA8B,EAAE;IAC1E,MAAM,OAAO,GAAS,CAAC,OAAgB,EAAE,EAAE;QACzC,MAAM,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC;QAC/B,MAAM,MAAM,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QAC3D,OAAO,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC;IACxC,CAAC,CAAC;IAEF,OAAO,YAAY,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;AACtC,CAAC,CAAC"}
|
|
@@ -13,6 +13,12 @@ function isRequestOptions(input) {
|
|
|
13
13
|
* @internal Determines the type of Request.Request.Input
|
|
14
14
|
*/
|
|
15
15
|
export function classifyRequestInput(input) {
|
|
16
|
+
if (typeof input === 'string') {
|
|
17
|
+
return {
|
|
18
|
+
type: 'parts',
|
|
19
|
+
input: { url: input },
|
|
20
|
+
};
|
|
21
|
+
}
|
|
16
22
|
if (isRequest(input)) {
|
|
17
23
|
return {
|
|
18
24
|
type: 'request',
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"classifyRequestInput.js","sourceRoot":"","sources":["../../src/Request/classifyRequestInput.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,uBAAoB;AAGxC,SAAS,WAAW,CAAC,KAAc;IACjC,OAAO,KAAK,YAAY,UAAU,CAAC,OAAO,CAAC;AAC7C,CAAC;AAED,SAAS,gBAAgB,CAAC,KAAsC;IAC9D,MAAM,eAAe,GAAG,cAAc,IAAI,KAAK,CAAC;IAEhD,IAAI,eAAe,EAAE,CAAC;QACpB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,oBAAoB,CAAC,KAAoB;IAiBvD,IAAI,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;QACrB,OAAO;YACL,IAAI,EAAE,SAAS;YACf,KAAK;SACN,CAAC;IACJ,CAAC;IAED,IAAI,WAAW,CAAC,KAAK,CAAC,EAAE,CAAC;QACvB,OAAO;YACL,IAAI,EAAE,WAAW;YACjB,KAAK;SACN,CAAC;IACJ,CAAC;IAED,IAAI,gBAAgB,CAAC,KAAK,CAAC,EAAE,CAAC;QAC5B,OAAO;YACL,IAAI,EAAE,SAAS;YACf,KAAK;SACN,CAAC;IACJ,CAAC;IAED,OAAO;QACL,IAAI,EAAE,OAAO;QACb,KAAK,EAAE,KAAK;KACb,CAAC;AACJ,CAAC"}
|
|
1
|
+
{"version":3,"file":"classifyRequestInput.js","sourceRoot":"","sources":["../../src/Request/classifyRequestInput.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,uBAAoB;AAGxC,SAAS,WAAW,CAAC,KAAc;IACjC,OAAO,KAAK,YAAY,UAAU,CAAC,OAAO,CAAC;AAC7C,CAAC;AAED,SAAS,gBAAgB,CAAC,KAAsC;IAC9D,MAAM,eAAe,GAAG,cAAc,IAAI,KAAK,CAAC;IAEhD,IAAI,eAAe,EAAE,CAAC;QACpB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,oBAAoB,CAAC,KAAoB;IAiBvD,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO;YACL,IAAI,EAAE,OAAO;YACb,KAAK,EAAE,EAAE,GAAG,EAAE,KAAK,EAAE;SACtB,CAAC;IACJ,CAAC;IAED,IAAI,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;QACrB,OAAO;YACL,IAAI,EAAE,SAAS;YACf,KAAK;SACN,CAAC;IACJ,CAAC;IAED,IAAI,WAAW,CAAC,KAAK,CAAC,EAAE,CAAC;QACvB,OAAO;YACL,IAAI,EAAE,WAAW;YACjB,KAAK;SACN,CAAC;IACJ,CAAC;IAED,IAAI,gBAAgB,CAAC,KAAK,CAAC,EAAE,CAAC;QAC5B,OAAO;YACL,IAAI,EAAE,SAAS;YACf,KAAK;SACN,CAAC;IACJ,CAAC;IAED,OAAO;QACL,IAAI,EAAE,OAAO;QACb,KAAK,EAAE,KAAK;KACb,CAAC;AACJ,CAAC"}
|
package/dist/Request/make.d.ts
CHANGED
|
@@ -13,6 +13,15 @@
|
|
|
13
13
|
* });
|
|
14
14
|
* ```
|
|
15
15
|
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { Request } from 'fx-fetch';
|
|
19
|
+
*
|
|
20
|
+
* // ┌─── Option.Option<Request.Request>
|
|
21
|
+
* // ▼
|
|
22
|
+
* const request = Request.make('https://example.com');
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
16
25
|
* @category Constructors
|
|
17
26
|
* @since 0.1.0
|
|
18
27
|
*/
|
package/dist/Request/make.js
CHANGED
|
@@ -15,6 +15,15 @@ import { unsafeMake } from "./unsafeMake.js";
|
|
|
15
15
|
* });
|
|
16
16
|
* ```
|
|
17
17
|
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* import { Request } from 'fx-fetch';
|
|
21
|
+
*
|
|
22
|
+
* // ┌─── Option.Option<Request.Request>
|
|
23
|
+
* // ▼
|
|
24
|
+
* const request = Request.make('https://example.com');
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
18
27
|
* @category Constructors
|
|
19
28
|
* @since 0.1.0
|
|
20
29
|
*/
|
package/dist/Request/make.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"make.js","sourceRoot":"","sources":["../../src/Request/make.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,EAAE,UAAU,EAAE,wBAAqB;AAE1C
|
|
1
|
+
{"version":3,"file":"make.js","sourceRoot":"","sources":["../../src/Request/make.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,EAAE,UAAU,EAAE,wBAAqB;AAE1C;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,CAAC,MAAM,IAAI,GAAG,aAAa,CAAC,UAAU,CAAC,CAAC"}
|
|
@@ -14,6 +14,15 @@ import type { Request } from "./Request.js";
|
|
|
14
14
|
* });
|
|
15
15
|
* ```
|
|
16
16
|
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { Request } from 'fx-fetch';
|
|
20
|
+
*
|
|
21
|
+
* // ┌─── Request.Request
|
|
22
|
+
* // ▼
|
|
23
|
+
* const request = Request.unsafeMake('https://example.com');
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
17
26
|
* @category Constructors
|
|
18
27
|
* @since 0.1.0
|
|
19
28
|
*/
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { isLeft } from 'effect/Either';
|
|
2
2
|
import { inputToRequestIntermediate } from "./inputToRequestIntermediate.js";
|
|
3
3
|
import { makeFromRequestIntermediate } from "./makeFromRequestIntermediate.js";
|
|
4
|
-
// TODO: Add tests for dual APIs
|
|
5
4
|
/**
|
|
6
5
|
* Creates a immutable Request object. Throws an error if the input is invalid.
|
|
7
6
|
*
|
|
@@ -17,6 +16,15 @@ import { makeFromRequestIntermediate } from "./makeFromRequestIntermediate.js";
|
|
|
17
16
|
* });
|
|
18
17
|
* ```
|
|
19
18
|
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* import { Request } from 'fx-fetch';
|
|
22
|
+
*
|
|
23
|
+
* // ┌─── Request.Request
|
|
24
|
+
* // ▼
|
|
25
|
+
* const request = Request.unsafeMake('https://example.com');
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
20
28
|
* @category Constructors
|
|
21
29
|
* @since 0.1.0
|
|
22
30
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"unsafeMake.js","sourceRoot":"","sources":["../../src/Request/unsafeMake.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AACvC,OAAO,EAAE,0BAA0B,EAAE,wCAAqC;AAC1E,OAAO,EAAE,2BAA2B,EAAE,yCAAsC;AAG5E
|
|
1
|
+
{"version":3,"file":"unsafeMake.js","sourceRoot":"","sources":["../../src/Request/unsafeMake.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AACvC,OAAO,EAAE,0BAA0B,EAAE,wCAAqC;AAC1E,OAAO,EAAE,2BAA2B,EAAE,yCAAsC;AAG5E;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,UAAU,UAAU,CAAC,KAAoB;IAC7C,MAAM,YAAY,GAAG,0BAA0B,CAAC,KAAK,CAAC,CAAC;IACvD,IAAI,MAAM,CAAC,YAAY,CAAC,EAAE,CAAC;QACzB,MAAM,YAAY,CAAC,IAAI,CAAC;IAC1B,CAAC;IAED,OAAO,2BAA2B,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;AACzD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fx-fetch",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"description": "Simple, immutable, clonable, and effect-based HTTP fetching.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"clonable",
|
|
@@ -66,12 +66,12 @@
|
|
|
66
66
|
"validate": "tsc"
|
|
67
67
|
},
|
|
68
68
|
"devDependencies": {
|
|
69
|
-
"@biomejs/biome": "^2.3.
|
|
70
|
-
"@effect/language-service": "^0.
|
|
69
|
+
"@biomejs/biome": "^2.3.14",
|
|
70
|
+
"@effect/language-service": "^0.73.1",
|
|
71
71
|
"@types/node": "^24",
|
|
72
|
-
"dpdm": "^
|
|
72
|
+
"dpdm": "^4.0.1",
|
|
73
73
|
"typescript": "^5.9.3",
|
|
74
|
-
"vitest": "^4.0.
|
|
74
|
+
"vitest": "^4.0.18",
|
|
75
75
|
"zshy": "^0.7.0"
|
|
76
76
|
},
|
|
77
77
|
"peerDependencies": {
|