Skip to content

Fetch.variables.paginatedFetchStream

fx-fetch


fx-fetch / Fetch / paginatedFetchStream

const paginatedFetchStream: {<A, E, R>(request, onResponse): Stream<A, FetchError | AbortError | NotAllowedError | NotOkError | E, Fetch | R>; <A, E, R>(onResponse): (request) => Stream<A, FetchError | AbortError | NotAllowedError | NotOkError | E, Fetch | R>; }

Defined in: packages/fx-fetch/src/Fetch/paginatedFetchStream.ts:83

<A, E, R>(request, onResponse): Stream<A, FetchError | AbortError | NotAllowedError | NotOkError | E, Fetch | R>

A

E

R

Request

OnResponse<A, E, R>

Stream<A, FetchError | AbortError | NotAllowedError | NotOkError | E, Fetch | R>

0.1.0

import { Effect, Option, Schema, Stream } from 'effect';
import { Fetch, Request, Response } from 'fx-fetch';
class Person extends Schema.Class<Person>('Person')({
id: Schema.Number,
name: Schema.NonEmptyString,
}) { }
const PayloadSchema = Schema.Struct({
nextToken: Schema.String.pipe(Schema.optional),
items: Person.pipe(Schema.Array),
});
const request = Request.unsafeMake({ url: './persons' });
// ┌─── Stream.Stream<
// │ Person[], // ◀︎── page emission
// │ | Fetch.FetchError | Fetch.AbortError | Fetch.NotAllowedError | Response.NotOkError
// │ | MalformedJsonError
// │ | ParseError,
// │ Fetch.Fetch
// │ >
// ▼
const program = Fetch.paginatedFetchStream(
request,
Effect.fn(function* (lastResponse) {
const payload = yield* Response.readJsonWithSchema(lastResponse, PayloadSchema);
const nextToken = Option.fromNullable(payload.nextToken);
const nextRequest = nextToken.pipe(
Option.map((token) => Request.setUrlSearchParam(request, 'token', token))
);
return {
pageEmission: payload.items, // ◀︎── Person[]
nextRequest, // ◀︎── Option.Option<Request.Request>
};
})
);

<A, E, R>(onResponse): (request) => Stream<A, FetchError | AbortError | NotAllowedError | NotOkError | E, Fetch | R>

A

E

R

OnResponse<A, E, R>

(request): Stream<A, FetchError | AbortError | NotAllowedError | NotOkError | E, Fetch | R>

Request

Stream<A, FetchError | AbortError | NotAllowedError | NotOkError | E, Fetch | R>

0.1.0

import { Effect, Option, Schema, Stream } from 'effect';
import { Fetch, Request, Response } from 'fx-fetch';
class Person extends Schema.Class<Person>('Person')({
id: Schema.Number,
name: Schema.NonEmptyString,
}) { }
const PayloadSchema = Schema.Struct({
nextToken: Schema.String.pipe(Schema.optional),
items: Person.pipe(Schema.Array),
});
const request = Request.unsafeMake({ url: './persons' });
// ┌─── Stream.Stream<
// │ Person[], // ◀︎── page emission
// │ | Fetch.FetchError | Fetch.AbortError | Fetch.NotAllowedError | Response.NotOkError
// │ | MalformedJsonError
// │ | ParseError,
// │ Fetch.Fetch
// │ >
// ▼
const program = Fetch.paginatedFetchStream(
request,
Effect.fn(function* (lastResponse) {
const payload = yield* Response.readJsonWithSchema(lastResponse, PayloadSchema);
const nextToken = Option.fromNullable(payload.nextToken);
const nextRequest = nextToken.pipe(
Option.map((token) => Request.setUrlSearchParam(request, 'token', token))
);
return {
pageEmission: payload.items, // ◀︎── Person[]
nextRequest, // ◀︎── Option.Option<Request.Request>
};
})
);

0.1.0

import { Effect, Option, Schema, Stream } from 'effect';
import { Fetch, Request, Response } from 'fx-fetch';
class Person extends Schema.Class<Person>('Person')({
id: Schema.Number,
name: Schema.NonEmptyString,
}) { }
const PayloadSchema = Schema.Struct({
nextToken: Schema.String.pipe(Schema.optional),
items: Person.pipe(Schema.Array),
});
const request = Request.unsafeMake({ url: './persons' });
// ┌─── Stream.Stream<
// │ Person[], // ◀︎── page emission
// │ | Fetch.FetchError | Fetch.AbortError | Fetch.NotAllowedError | Response.NotOkError
// │ | MalformedJsonError
// │ | ParseError,
// │ Fetch.Fetch
// │ >
// ▼
const program = Fetch.paginatedFetchStream(
request,
Effect.fn(function* (lastResponse) {
const payload = yield* Response.readJsonWithSchema(lastResponse, PayloadSchema);
const nextToken = Option.fromNullable(payload.nextToken);
const nextRequest = nextToken.pipe(
Option.map((token) => Request.setUrlSearchParam(request, 'token', token))
);
return {
pageEmission: payload.items, // ◀︎── Person[]
nextRequest, // ◀︎── Option.Option<Request.Request>
};
})
);