@nyc-transit-kit/cli 0.1.4 → 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,103 +0,0 @@
1
- import type { Soda3ExportFormat } from "@nyc-transit-kit/contracts/soda3"
2
- import { buildExportUrl, buildQueryUrl } from "@nyc-transit-kit/soda3/endpoints"
3
- import * as Effect from "effect/Effect"
4
- import { atomicWrite } from "../files"
5
- import { runEffect } from "./shared"
6
- import type { ApiFamily, CommandContext } from "./types"
7
-
8
- type DryRunExtra = Readonly<Record<string, unknown>>
9
-
10
- export const fileWriteError = (cause: unknown) => ({
11
- _tag: "FileWriteError",
12
- message: cause instanceof Error ? cause.message : "File write failed."
13
- })
14
-
15
- const optionalHeader = (headers: Headers, name: string) => headers.get(name) ?? undefined
16
-
17
- const parseContentLength = (value: string | undefined) => {
18
- if (value === undefined) {
19
- return undefined
20
- }
21
-
22
- const parsed = Number(value)
23
- return Number.isInteger(parsed) && parsed >= 0 ? parsed : undefined
24
- }
25
-
26
- export const writeSoda3QueryDryRun = (config: {
27
- readonly context: CommandContext
28
- readonly apiFamily: ApiFamily
29
- readonly tokens: ReadonlyArray<string>
30
- readonly domain: string
31
- readonly datasetId: string
32
- readonly query: string
33
- readonly extra?: DryRunExtra
34
- }) =>
35
- runEffect(
36
- config.context,
37
- config.apiFamily,
38
- config.tokens,
39
- buildQueryUrl({
40
- domain: config.domain,
41
- datasetId: config.datasetId,
42
- query: config.query
43
- }).pipe(
44
- Effect.map((url) => ({
45
- dryRun: true,
46
- method: "POST",
47
- url: url.toString(),
48
- ...config.extra,
49
- body: {
50
- query: config.query
51
- }
52
- }))
53
- )
54
- )
55
-
56
- export const writeSoda3ExportDryRun = (config: {
57
- readonly context: CommandContext
58
- readonly apiFamily: ApiFamily
59
- readonly tokens: ReadonlyArray<string>
60
- readonly domain: string
61
- readonly datasetId: string
62
- readonly format: Soda3ExportFormat
63
- readonly output: string
64
- readonly extra?: DryRunExtra
65
- }) =>
66
- runEffect(
67
- config.context,
68
- config.apiFamily,
69
- config.tokens,
70
- buildExportUrl({
71
- domain: config.domain,
72
- datasetId: config.datasetId,
73
- format: config.format
74
- }).pipe(
75
- Effect.map((url) => ({
76
- dryRun: true,
77
- method: "POST",
78
- url: url.toString(),
79
- ...config.extra,
80
- output: config.output
81
- }))
82
- )
83
- )
84
-
85
- export const writeResponseToFile = (output: string, response: Response) =>
86
- Effect.gen(function* () {
87
- const contentType = optionalHeader(response.headers, "content-type")
88
- const contentLength = parseContentLength(optionalHeader(response.headers, "content-length"))
89
-
90
- yield* Effect.tryPromise({
91
- try: () => atomicWrite(output, response),
92
- catch: fileWriteError
93
- })
94
- const byteLength = yield* Effect.sync(() => Bun.file(output).size)
95
-
96
- return {
97
- output,
98
- status: response.status,
99
- byteLength,
100
- ...(contentType === undefined ? {} : { contentType }),
101
- ...(contentLength === undefined ? {} : { contentLength })
102
- }
103
- })
@@ -1,33 +0,0 @@
1
- import type { GtfsRealtimeDecoder } from "@nyc-transit-kit/mta/gtfs-realtime"
2
- import type { Soda3ClientConfig } from "@nyc-transit-kit/soda3/client"
3
- import * as Context from "effect/Context"
4
- import type * as Layer from "effect/Layer"
5
- import type * as HttpClient from "effect/unstable/http/HttpClient"
6
- import type { CliMetaApiFamily } from "../output"
7
-
8
- export type ApiFamily = CliMetaApiFamily
9
-
10
- export type CommandResult<A> =
11
- | {
12
- readonly _tag: "Failure"
13
- readonly error: unknown
14
- }
15
- | {
16
- readonly _tag: "Success"
17
- readonly data: A
18
- }
19
-
20
- export type CliRuntime = {
21
- readonly soda3Layer: Layer.Layer<Soda3ClientConfig | HttpClient.HttpClient>
22
- readonly mtaLayer: Layer.Layer<HttpClient.HttpClient>
23
- readonly mtaRealtimeLayer: Layer.Layer<HttpClient.HttpClient | GtfsRealtimeDecoder>
24
- }
25
-
26
- export type CommandContext = {
27
- readonly runtime: CliRuntime
28
- readonly writeJson: (output: unknown) => void
29
- }
30
-
31
- export class CliCommandContext extends Context.Service<CliCommandContext, CommandContext>()(
32
- "@nyc-transit-kit/cli/CliCommandContext"
33
- ) {}
package/src/config.ts DELETED
@@ -1,31 +0,0 @@
1
- import { MtaHttpLive, mtaConfigLayer } from "@nyc-transit-kit/mta/client"
2
- import { GtfsRealtimeDecoder } from "@nyc-transit-kit/mta/gtfs-realtime"
3
- import { Soda3ClientConfig, Soda3HttpLive } from "@nyc-transit-kit/soda3/client"
4
- import * as Layer from "effect/Layer"
5
-
6
- export interface Soda3CliLayerConfig {
7
- readonly appToken?: string
8
- }
9
-
10
- export const soda3CliLayer = (config: Soda3CliLayerConfig) =>
11
- Layer.mergeAll(
12
- Layer.succeed(Soda3ClientConfig)({
13
- appToken: config.appToken,
14
- retryTimes: 2,
15
- retryDelayMs: 250
16
- }),
17
- Soda3HttpLive
18
- )
19
-
20
- const mtaCliConfigLayer = mtaConfigLayer({
21
- retryTimes: 2,
22
- retryDelayMs: 250
23
- })
24
-
25
- export const mtaCliLayer = Layer.mergeAll(MtaHttpLive, mtaCliConfigLayer)
26
-
27
- export const mtaRealtimeCliLayer = Layer.mergeAll(
28
- MtaHttpLive,
29
- mtaCliConfigLayer,
30
- GtfsRealtimeDecoder.Live
31
- )
package/src/output.ts DELETED
@@ -1,39 +0,0 @@
1
- import { releaseVersion, schemaVersion } from "@nyc-transit-kit/contracts/metadata"
2
- import { buildCommit, buildTarget } from "./build-info"
3
-
4
- export const packageVersion = releaseVersion
5
- export const packageBuildCommit = buildCommit
6
- export const packageBuildTarget = buildTarget
7
-
8
- const currentGeneratedAt = () => new Date().toISOString()
9
-
10
- export type CliMetaApiFamily = "socrata" | "mta" | "nyc-open-data" | "nyc-dot" | "cli"
11
-
12
- export const successEnvelope = (apiFamily: CliMetaApiFamily, data: unknown) => ({
13
- ok: true,
14
- data,
15
- meta: {
16
- apiFamily,
17
- generatedAt: currentGeneratedAt(),
18
- schemaVersion
19
- }
20
- })
21
-
22
- export const errorEnvelope = (
23
- apiFamily: CliMetaApiFamily,
24
- error: {
25
- readonly code: string
26
- readonly message: string
27
- readonly provider?: string
28
- readonly retryable?: boolean
29
- readonly command?: string
30
- }
31
- ) => ({
32
- ok: false,
33
- error,
34
- meta: {
35
- apiFamily,
36
- generatedAt: currentGeneratedAt(),
37
- schemaVersion
38
- }
39
- })