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
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# In order to test the Zenodo API you need to create an access token
# First create an account on https://sandbox.zenodo.org/
# Then create a personal access token https://sandbox.zenodo.org/account/settings/applications/

ACCESS_TOKEN=ChangeMe
29 changes: 29 additions & 0 deletions .github/workflows/lactame.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Deploy build on lactame.com

on:
release:
types: [published]

env:
NODE_VERSION: 22.x

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Get package name
run: echo "PACKAGE_NAME=$(jq .name package.json | tr -d '"')" >> $GITHUB_ENV
- uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
- name: Install dependencies
run: npm install
- name: Build project
run: npm run build
- name: Deploy to lactame.com
uses: zakodium/lactame-action@v1
with:
token: ${{ secrets.LACTAME_TOKEN }}
name: ${{ env.PACKAGE_NAME }}
folder: dist
19 changes: 19 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Node.js CI

on:
push:
branches:
- main
pull_request:

jobs:
nodejs:
# Documentation: https://github.com/zakodium/workflows#nodejs-ci
uses: zakodium/workflows/.github/workflows/nodejs.yml@nodejs-v1
with:
lint-check-types: true
# Zenodo API is a little bit touchy, so we only test with old version of Node.js
# More over this packages requires at least Node.js 20
node-version-matrix: '[20]'
secrets:
env: ACCESS_TOKEN=${{ secrets.ACCESS_TOKEN }}
16 changes: 16 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Release

on:
push:
branches:
- main

jobs:
release:
# Documentation: https://github.com/zakodium/workflows#release
uses: zakodium/workflows/.github/workflows/release.yml@release-v1
with:
npm: true
secrets:
github-token: ${{ secrets.BOT_TOKEN }}
npm-token: ${{ secrets.NPM_BOT_TOKEN }}
29 changes: 29 additions & 0 deletions .github/workflows/typedoc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Deploy TypeDoc on GitHub pages

on:
workflow_dispatch:
release:
types: [published]

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 22.x
- name: Install dependencies
run: npm install
- name: Build documentation
uses: zakodium/typedoc-action@v2
with:
entry: src/index.ts

- name: Deploy to GitHub pages
uses: JamesIves/github-pages-deploy-action@releases/v4
with:
token: ${{ secrets.BOT_TOKEN }}
branch: gh-pages
folder: docs
clean: true
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,9 @@ jspm_packages
.node_repl_history

testAuth.json

.env

lib
lib-esm
dist
7 changes: 7 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"arrowParens": "always",
"semi": true,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "all"
}
File renamed without changes.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,50 @@
# zenodo

Node.js library to access the Zenodo API

## Zenodo API Documentation

https://developers.zenodo.org/

## Testing the project

In order to test the project you will have to create a .env file that contains an API key to the sandbox.

https://developers.zenodo.org/#authentication

## Usage

```js
// by default we set the host to 'sandbox.zenodo.org' so that you can easily play around with this library without damage
const zenodo = new Zenodo({ accessToken, host: 'zenodo.org' });

// retrieve the list of all the depositions
const depositions = await zenodo.listDepositions();
for (const deposition of depoositions) {
console.log(deposition);
}

// create a new deposition
const deposition = zenodo.createDeposition({
upload_type: 'dataset',
description: 'test',
access_right: 'open',
creators: [
{
name: 'test',
},
],
});
const firstDeposition = await zenodo.createDeposition(depositionMetadata);

const firstFile = new File(['Hello, world!'], 'example.txt', {
type: 'text/plain',
});
const newFile = await firstDeposition.createFile(firstFile);

const secondFile = new File(['Hello, world 2!'], 'example2.txt', {
type: 'text/plain',
});
```

## Development
1 change: 0 additions & 1 deletion abc.txt

This file was deleted.

14 changes: 14 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import cheminfo from 'eslint-config-cheminfo-typescript';

export default [
...cheminfo,
{
languageOptions: {
globals: {},
},
rules: {
'@typescript-eslint/naming-convention': 'off',
camelcase: 'off',
},
},
];
43 changes: 35 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,53 @@
"name": "zenodo",
"version": "1.0.2",
"description": "Node.js library to access the Zenodo API",
"main": "src/index.js",
"main": "./lib/index.js",
"module": "./lib-esm/index.js",
"types": "./lib/index.d.ts",
"files": [
"src",
"lib",
"lib-esm"
],
"scripts": {
"test": "echo OK"
"build": "npm run tsc-esm && cheminfo-build --entry lib-esm/index.js --root Zenodo",
"build-watch": "fswatch -o src | xargs -n1 -I{} npm run build",
"check-types": "tsc --noEmit",
"clean": "rimraf lib lib-esm",
"eslint": "eslint src",
"eslint-fix": "npm run eslint -- --fix",
"prepack": "npm run tsc",
"prettier": "prettier --check src",
"prettier-write": "prettier --write src",
"test": "npm run test-only && npm run eslint && npm run prettier && npm run check-types",
"test-only": "vitest run --coverage",
"tsc": "npm run clean && npm run tsc-cjs && npm run tsc-esm",
"tsc-cjs": "tsc --project tsconfig.cjs.json",
"tsc-esm": "tsc --project tsconfig.esm.json"
},
"repository": {
"type": "git",
"url": "git+https://github.com/cheminfo/zenodo.git"
},
"files": [
"src"
],
"keywords": [],
"author": "Michaël Zasso",
"license": "MIT",
"bugs": {
"url": "https://github.com/cheminfo/zenodo/issues"
},
"homepage": "https://github.com/cheminfo/zenodo#readme",
"dependencies": {
"axios": "^0.21.1"
"devDependencies": {
"@types/node": "^22.13.4",
"@vitest/coverage-v8": "^3.0.5",
"cheminfo-build": "^1.2.1",
"dotenv": "^16.4.7",
"eslint": "^9.20.1",
"eslint-config-cheminfo-typescript": "^17.0.0",
"prettier": "^3.5.1",
"rimraf": "^6.0.1",
"vitest": "^3.0.5"
},
"devDependencies": {}
"dependencies": {
"zod": "^3.24.2"
}
}
32 changes: 0 additions & 32 deletions samples/entry.json

This file was deleted.

16 changes: 0 additions & 16 deletions samples/file.json

This file was deleted.

6 changes: 0 additions & 6 deletions server.js

This file was deleted.

68 changes: 68 additions & 0 deletions src/Zenodo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { Deposition } from './depositions/Deposition';
import type { ListDepositionsOptions } from './depositions/ListDepositionsOptions';
import type { DepositionMetadata } from './depositions/depositionSchema';
import { fetchZenodo } from './fetchZenodo';

interface ZenodoOptions {
accessToken: string;
host?: string;
}
export class Zenodo {
host: string;
accessToken: string;
baseURL: string;

constructor(options: ZenodoOptions) {
const { accessToken, host = 'sandbox.zenodo.org' } = options;
this.host = host;
this.baseURL = `https://${host}/api/`;
if (!accessToken) {
throw new Error('accessToken is required');
}
this.accessToken = accessToken;
}

async listDepositions(
options: ListDepositionsOptions = {},
): Promise<Deposition[]> {
// all the values must be string
const optionsWithStrings = Object.fromEntries(
Object.entries(options).map(([key, value]) => [key, String(value)]),
);
const response = await fetchZenodo(this, {
route: 'deposit/depositions',
searchParams: optionsWithStrings,
});
const depositions = (await response.json()) as unknown[];
return depositions.map(
(deposition: unknown) => new Deposition(this, deposition),
);
}

async createDeposition(metadata: DepositionMetadata): Promise<Deposition> {
const response = await fetchZenodo(this, {
route: 'deposit/depositions',
expectedStatus: 201,
method: 'POST',
body: JSON.stringify({ metadata }),
});
const deposition = new Deposition(this, await response.json());
return deposition;
}

async retrieveDeposition(id: number): Promise<Deposition> {
const response = await fetchZenodo(this, {
route: `deposit/depositions/${id}`,
});
const deposition = await response.json();
return new Deposition(this, deposition);
}

Check warning on line 59 in src/Zenodo.ts

View check run for this annotation

Codecov / codecov/patch

src/Zenodo.ts#L54-L59

Added lines #L54 - L59 were not covered by tests

async deleteDeposition(id: number): Promise<void> {
await fetchZenodo(this, {
method: 'DELETE',
route: `deposit/depositions/${id}`,
expectedStatus: 204,
});
}
}
Loading