Skip to content

Commit 07bb18e

Browse files
committed
fix coderabbit suggestions
1 parent 58d3eca commit 07bb18e

4 files changed

Lines changed: 18 additions & 12 deletions

File tree

config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ config(); // loads .env
55
import { z } from "zod";
66

77
const EnvSchema = z.object({
8-
SOURCE_CKAN_URL: z.string(),
8+
SOURCE_CKAN_URL: z.string().url(),
99
SOURCE_CKAN_API_KEY: z.string().optional(),
1010
SOURCE_CKAN_ORG_ID: z.string().optional(),
1111

12-
PORTALJS_CKAN_URL: z.string(),
12+
PORTALJS_CKAN_URL: z.string().url(),
1313
PORTALJS_CKAN_API_KEY: z.string().min(1),
1414
PORTALJS_ORG_ID: z.string().min(1),
1515

1616
CONCURRENCY: z.coerce.number().default(4),
1717
RATE_LIMIT_RPS: z.coerce.number().default(2),
1818
RETRY_MAX_ATTEMPTS: z.coerce.number().default(2),
1919
RETRY_BASE_MS: z.coerce.number().default(500),
20-
DRY_RUN: z.preprocess(v => String(v).toLowerCase() === "true", z.boolean()).default(false),
20+
DRY_RUN: z.coerce.boolean().default(false),
2121

2222
SINCE_ISO: z.string().optional(),
2323
STATE_FILE: z.string().default(".harvest_state.json"),

gen-schema.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,34 @@ import fs from "fs";
33
import { env } from "./config";
44
import { capitalize } from "./src/utils";
55

6-
7-
86
interface SchemaField {
97
field_name: string;
108
choices?: Array<{ value: string; label: string }>;
11-
validators?: string;
9+
validators?: string | string[];
1210
}
1311

1412
async function generateTypes(type: string, schemaUrl: string) {
1513
const res = await fetch(schemaUrl);
14+
if (!res.ok) {
15+
const text = await res.text().catch(() => "");
16+
throw new Error(
17+
`Failed to fetch schema from ${schemaUrl}: ${res.status} ${
18+
res.statusText
19+
} ${text.slice(0, 300)}`
20+
);
21+
}
1622
const data = await res.json();
1723
const { dataset_fields, resource_fields } = data.result;
1824

1925
const datasetProps = dataset_fields.map((f: SchemaField) => {
2026
let tsType = "string";
2127

2228
if (f.choices) {
23-
tsType = f.choices.map((c) => `"${c.value}"`).join(" | ");
29+
tsType = f.choices.map((c) => JSON.stringify(String(c.value))).join(" | ");
2430
}
2531

26-
const isRequired =
27-
f.validators &&
28-
(f.validators.includes("not_empty") ||
29-
f.validators.includes("scheming_required"));
32+
const validators = Array.isArray(f.validators) ? f.validators : (f.validators ? [f.validators] : []);
33+
const isRequired = validators.some(v => v.includes("not_empty") || v.includes("scheming_required"));
3034

3135
return ` ${f.field_name}${isRequired ? "" : "?"}: ${tsType};`;
3236
});
@@ -46,6 +50,7 @@ ${resourceProps.join("\n")}
4650
}
4751
`;
4852

53+
fs.mkdirSync("schemas", { recursive: true });
4954
fs.writeFileSync(`schemas/${type}-schema.d.ts`, content);
5055
console.log(`✅ Types generated in schemas/${type}-schema.d.ts`);
5156
}

src/map.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function mapCkanToPortalJS(ds: SourceSchema, owner_org: string): TargetSc
1111
name: r.name,
1212
url: r.url,
1313
format: r.format,
14-
id:""
14+
...(r.id ? { id: r.id } : {}),
1515
})),
1616

1717
language:ds.language || "EN"

src/source.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export async function* iterSourcePackages(pageSize = 25) {
3232
action,
3333
{
3434
ckanUrl: env.SOURCE_CKAN_URL,
35+
apiKey: env.SOURCE_CKAN_API_KEY || undefined,
3536
}
3637
);
3738

0 commit comments

Comments
 (0)