Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: rebuilt project in TS with API validation with Zod #10

Merged
merged 23 commits into from
Feb 21, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 17 additions & 26 deletions src/ZenodoFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,39 @@

import type { Zenodo } from './Zenodo';

export class ZenodoFile {
private zenodo: Zenodo;
id: string;
filename: string;
filesize: number;
checksum: string; // md5
links: {
download: string;
self: string;
};
// Define the Zod schema
const zenodoFileSchema = z.object({
id: z.string(),
filename: z.string(),
filesize: z.number(),
checksum: z.string(),
links: z.object({
download: z.string().url(),
self: z.string().url(),
}),
});

type ZenodoFileType = z.infer<typeof zenodoFileSchema>;

static schema = z.object({
id: z.string(),
filename: z.string(),
filesize: z.number(),
checksum: z.string().regex(/^[a-fA-F0-9]{32}$/, 'Invalid MD5 checksum'),
links: z.object({
download: z.string().url(),
self: z.string().url(),
}),
});
export class ZenodoFile implements ZenodoFileType {

Check failure on line 19 in src/ZenodoFile.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-check-types

Class 'ZenodoFile' incorrectly implements interface '{ id: string; filename: string; filesize: number; checksum: string; links: { download: string; self: string; }; }'.
private zenodo: Zenodo;

constructor(zenodo: Zenodo, file: unknown) {
const validatedFile = ZenodoFile.schema.parse(file);
const validatedFile = zenodoFileSchema.parse(file);
this.zenodo = zenodo;
this.id = validatedFile.id;
this.filename = validatedFile.filename;
this.filesize = validatedFile.filesize;
this.checksum = validatedFile.checksum;
this.links = validatedFile.links;
Object.assign(this, validatedFile);
}

async getContentResponse() {
const link = this.links.download;

Check failure on line 29 in src/ZenodoFile.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-check-types

Property 'links' does not exist on type 'ZenodoFile'.
const response = await fetch(link, {
headers: {
Authorization: `Bearer ${this.zenodo.accessToken}`,
},
});
if (!response.ok) {
throw new Error(`Failed to fetch ${link}`);
}

Check warning on line 37 in src/ZenodoFile.ts

View check run for this annotation

Codecov / codecov/patch

src/ZenodoFile.ts#L36-L37

Added lines #L36 - L37 were not covered by tests
return response;
}
}
Loading