|
| 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