@nyc-transit-kit/soda3 0.1.3 → 0.2.0

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.
@@ -1,42 +0,0 @@
1
- import * as Effect from "effect/Effect"
2
- import * as Stream from "effect/Stream"
3
- import * as FetchHttpClient from "effect/unstable/http/FetchHttpClient"
4
- import type * as HttpClientError from "effect/unstable/http/HttpClientError"
5
- import type * as HttpClientResponse from "effect/unstable/http/HttpClientResponse"
6
- import { ProviderHttpError } from "../errors"
7
-
8
- export const Soda3HttpLive = FetchHttpClient.layer
9
-
10
- export const retryableStatus = (status: number) => status === 429 || status >= 500
11
-
12
- const httpStatusText = (status: number) => `HTTP ${status}`
13
-
14
- export const toWebResponse = (response: HttpClientResponse.HttpClientResponse) =>
15
- Effect.map(
16
- Stream.toReadableStreamEffect(response.stream),
17
- (body) =>
18
- new Response(body, {
19
- status: response.status,
20
- headers: response.headers
21
- })
22
- )
23
-
24
- export const mapHttpClientError = (operation: string, error: HttpClientError.HttpClientError) => {
25
- const response = error.response
26
-
27
- if (response !== undefined) {
28
- return ProviderHttpError.make({
29
- operation,
30
- status: response.status,
31
- statusText: httpStatusText(response.status),
32
- retryable: retryableStatus(response.status)
33
- })
34
- }
35
-
36
- return ProviderHttpError.make({
37
- operation,
38
- status: 0,
39
- statusText: error.message,
40
- retryable: true
41
- })
42
- }
@@ -1,56 +0,0 @@
1
- import {
2
- Soda3CatalogSearchResponse,
3
- Soda3QueryResponse,
4
- Soda3Row
5
- } from "@nyc-transit-kit/contracts/soda3"
6
- import * as Effect from "effect/Effect"
7
- import * as Schema from "effect/Schema"
8
- import type * as HttpClientResponse from "effect/unstable/http/HttpClientResponse"
9
- import { ProviderContractError } from "../errors"
10
-
11
- const providerRows = Schema.Array(Soda3Row)
12
-
13
- export const decodeJson = (operation: string, response: HttpClientResponse.HttpClientResponse) =>
14
- response.json.pipe(
15
- Effect.catchTag("HttpClientError", () =>
16
- Effect.fail(
17
- ProviderContractError.make({
18
- operation,
19
- message: "Provider response was not valid JSON"
20
- })
21
- )
22
- )
23
- )
24
-
25
- export const decodeQueryResponse = (input: unknown) =>
26
- Schema.decodeUnknownEffect(Soda3QueryResponse)(input).pipe(
27
- Effect.catchTag("SchemaError", (error) =>
28
- Schema.decodeUnknownEffect(providerRows)(input).pipe(
29
- Effect.map((rows) =>
30
- Soda3QueryResponse.make({
31
- rows
32
- })
33
- ),
34
- Effect.catchTag("SchemaError", () =>
35
- Effect.fail(
36
- ProviderContractError.make({
37
- operation: "query",
38
- message: error.message
39
- })
40
- )
41
- )
42
- )
43
- )
44
- )
45
-
46
- export const decodeCatalogSearchResponse = (input: unknown) =>
47
- Schema.decodeUnknownEffect(Soda3CatalogSearchResponse)(input).pipe(
48
- Effect.catchTag("SchemaError", (error) =>
49
- Effect.fail(
50
- ProviderContractError.make({
51
- operation: "catalog",
52
- message: error.message
53
- })
54
- )
55
- )
56
- )
@@ -1,41 +0,0 @@
1
- import * as Context from "effect/Context"
2
- import * as Layer from "effect/Layer"
3
- import * as FetchHttpClient from "effect/unstable/http/FetchHttpClient"
4
- import { Soda3HttpLive } from "./http"
5
-
6
- export interface Soda3ClientConfigShape {
7
- readonly appToken?: string
8
- readonly retryTimes?: number
9
- readonly timeoutMs?: number
10
- }
11
-
12
- export interface Soda3LayerOptions extends Soda3ClientConfigShape {
13
- readonly fetch?: typeof fetch
14
- }
15
-
16
- export class Soda3ClientConfig extends Context.Service<Soda3ClientConfig, Soda3ClientConfigShape>()(
17
- "@nyc-transit-kit/soda3/Soda3ClientConfig"
18
- ) {
19
- static readonly Default = Layer.succeed(Soda3ClientConfig)({
20
- retryTimes: 0
21
- })
22
- }
23
-
24
- export const soda3ConfigLayer = (options: Soda3ClientConfigShape = {}) =>
25
- Layer.succeed(Soda3ClientConfig)({
26
- retryTimes: options.retryTimes ?? 0,
27
- ...(options.appToken === undefined ? {} : { appToken: options.appToken }),
28
- ...(options.timeoutMs === undefined ? {} : { timeoutMs: options.timeoutMs })
29
- })
30
-
31
- export const soda3FetchLayer = (fetchImplementation?: typeof fetch) =>
32
- fetchImplementation === undefined
33
- ? Soda3HttpLive
34
- : FetchHttpClient.layer.pipe(
35
- Layer.provide(Layer.succeed(FetchHttpClient.Fetch, fetchImplementation))
36
- )
37
-
38
- export const soda3Layer = (options: Soda3LayerOptions = {}) =>
39
- Layer.mergeAll(soda3ConfigLayer(options), soda3FetchLayer(options.fetch))
40
-
41
- export const Soda3Live = soda3Layer()