Skip to content

Commit ecd16a5

Browse files
authored
Merge pull request #1367 from datopian/feat/ckan-api-client-js
ckan-api-client-js
2 parents 970e4ba + 7fe355f commit ecd16a5

File tree

14 files changed

+7486
-1567
lines changed

14 files changed

+7486
-1567
lines changed

.changeset/lemon-ducks-shake.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@portaljs/ckan-api-client-js': minor
3+
---
4+
5+
Initial public release

package-lock.json

Lines changed: 2914 additions & 1567 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# database
12+
/prisma/db.sqlite
13+
/prisma/db.sqlite-journal
14+
15+
# next.js
16+
/.next/
17+
/out/
18+
next-env.d.ts
19+
20+
# production
21+
/build
22+
23+
# misc
24+
.DS_Store
25+
*.pem
26+
27+
# debug
28+
npm-debug.log*
29+
yarn-debug.log*
30+
yarn-error.log*
31+
.pnpm-debug.log*
32+
33+
# local env files
34+
# do not commit any .env files to git, except for the .env.example file. https://create.t3.gg/en/usage/env-variables#using-environment-variables
35+
.env
36+
.env*.local
37+
38+
# vercel
39+
.vercel
40+
41+
# typescript
42+
*.tsbuildinfo
43+
44+
/dist
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"$schema": "https://json.schemastore.org/mocharc.json",
3+
"require": "tsx",
4+
"spec": ["test/**/*.ts"]
5+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# @portaljs/ckan-api-client-js
2+
3+
## 1.3.3
4+
5+
### Patch Changes
6+
7+
- 3bb69b4: Dual build as ES module and CommonJS so that exports work properly on Next.js builds
8+
9+
## 1.3.2
10+
11+
### Patch Changes
12+
13+
- 1bc2193: Export CkanRequest as default export and CkanRequestError as named export
14+
15+
## 1.3.1
16+
17+
### Patch Changes
18+
19+
- be275c4: CkanRequestError custom error wasn't being exported
20+
21+
## 1.3.0
22+
23+
### Minor Changes
24+
25+
- b1a75f2: Post and get methods are now generic, so that response can be properly typed"
26+
27+
## 1.2.1
28+
29+
### Patch Changes
30+
31+
- 6a4eafe: Fix get and post method being exported without types
32+
33+
## 1.2.0
34+
35+
### Minor Changes
36+
37+
- 9ab2d02: Implemented apiKey and formData options
38+
39+
## 1.1.0
40+
41+
### Minor Changes
42+
43+
- dde4357: Add support to Authorization Error
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
## CKAN API client - JavaScript
2+
3+
Among other JS clients publicly available, the objectives of this one are:
4+
5+
- **To be completely flexible in terms of which CKAN actions are supported**, so users specify which action should be called by its name rather than my importing and calling a method implemented specifically for the given action. This ensure that all core and custom actions are supported, including all possible parameters.
6+
- **To reduce repetition when calling CKAN actions**, by reading global configurations on environemnt variables (such as the CKAN URL) and having common configurations by default (e.g. all requests by default will have the "content-type" header set to "application/json", avoiding that requests are sent without it and avoing that this has to be repeated everywhere).
7+
- **To properly handle errors**, by properly detecting when an error happened and throwing an error with a useful message that can be shown to the final users.
8+
- **To expose the underlying request properties**, so that anything can be customized e.g. headers
9+
10+
_**NOTE:**_ developed mainly to be used on Next.js projects.
11+
12+
## Usage
13+
14+
Install the package on the project with (or `npm link` for local development):
15+
16+
```
17+
npm i @portaljs/ckan-api-client-js
18+
```
19+
20+
Set the following environment variables on your project:
21+
22+
```bash
23+
NEXT_PUBLIC_CKAN_URL=http://ckan.com # <= This should be updated
24+
```
25+
26+
Import the client with:
27+
28+
```javascript
29+
import CkanRequest from "ckan-api-client-js";
30+
```
31+
32+
### Methods
33+
34+
`CkanRequest` exports 2 main methods:
35+
36+
- `CkanRequest.get("action_name", options)`
37+
- `CkanRequest.post("action_name", options)`
38+
39+
`options` may have the following properties:
40+
41+
- `apiKey` - CKAN API key for authorization
42+
- `headers` - Request headers. E.g. `{ "Authorization": "api_token" }`
43+
- `json` - JSON body for POST requests. E.g. `{ "id": "123" }`
44+
- `formData` - formData for POST requests
45+
46+
### Examples
47+
48+
#### `package_show`
49+
50+
```javascript
51+
const dataset = await CkanRequest.get(
52+
"package_show?id=123",
53+
{
54+
apiKey: "my-token"
55+
}
56+
);
57+
```
58+
59+
#### `package_patch`
60+
61+
```javascript
62+
const dataset = await CkanRequest.post(
63+
"package_patch",
64+
{
65+
headers: {
66+
Authorization: "apikey",
67+
},
68+
json: { "id": "123", "title": "My new title" },
69+
}
70+
);
71+
```
72+
73+
#### Error handling
74+
75+
If an exception happens, simply catch that and show its message to the user. Example:
76+
77+
```javascript
78+
try {
79+
const dataset = CkanRequest.get("package_show?id=123")
80+
} catch (e) {
81+
alert(e.message) // E.g. "Dataset not found"
82+
}
83+
84+
```
85+
86+
## Development
87+
88+
Currently, `npm link` is being used for development purposes.
89+
90+
To do so, simply build (`npm run build`) the project and then link it (`npm link ...`) on another project to test changes.
91+

0 commit comments

Comments
 (0)