observation-js
Version:
A fully-typed TypeScript client for the waarneming.nl API.
59 lines (58 loc) • 2.04 kB
JavaScript
export class Exports {
#client;
/**
* @internal
*/
constructor(client) {
this.#client = client;
}
/**
* Fetches a list of the authenticated user's observation exports.
*
* @returns A promise that resolves to a paginated list of the user's exports.
* @throws {AuthenticationError} If the request is not authenticated.
* @throws {ApiError} If the request fails.
*/
async list() {
return this.#client.request('exports/');
}
/**
* Fetches the details of a single export by its ID.
* The export must belong to the authenticated user.
*
* @param exportId - The unique identifier for the export.
* @returns A promise that resolves to the export object.
* @throws {AuthenticationError} If the request is not authenticated.
* @throws {ApiError} If the request fails.
*/
async get(exportId) {
return this.#client.request(`exports/${exportId}`);
}
/**
* Starts a new observation export job.
* This sends a request to the server to begin generating an export file.
* The status of the job can be tracked via the `get` method.
*
* @param options - The export options, specifying the type, format, and filters.
* @returns A promise that resolves to an object containing the new export's ID and status URL.
* @throws {AuthenticationError} If the request is not authenticated.
* @throws {ApiError} If the request fails.
*/
async start(options) {
const bodyParams = {};
for (const key in options) {
const value = options[key];
if (value !== undefined) {
bodyParams[key] = String(value);
}
}
const body = new URLSearchParams(bodyParams).toString();
return this.#client.request('exports/', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body,
});
}
}