From f60159fb723434b6bf8a43d73a5e490e3aafc9d0 Mon Sep 17 00:00:00 2001 From: Travis Dillon Date: Wed, 14 Feb 2024 15:19:25 -0500 Subject: [PATCH 1/2] Use TC39 stage 3 Decorators proposal --- deno.json | 3 -- src/mapbox.ts | 83 +++++++++++++++++++++------------------------------ 2 files changed, 34 insertions(+), 52 deletions(-) diff --git a/deno.json b/deno.json index 6b6293d..19ac121 100644 --- a/deno.json +++ b/deno.json @@ -22,8 +22,5 @@ "test-stats": "deno run --allow-env=MAPBOX_USERNAME,MAPBOX_TOKEN --allow-read=. mapper.js stats --root sample-data/horseshoe", "test-version": "deno run --allow-env=MAPBOX_USERNAME,MAPBOX_TOKEN --allow-read=. mapper.js version", "test": "deno test --allow-read=. --allow-env=MAPBOX_USERNAME,MAPBOX_TOKEN --coverage --cached-only --junit-path=coverage/junit.xml" - }, - "compilerOptions": { - "experimentalDecorators": true } } \ No newline at end of file diff --git a/src/mapbox.ts b/src/mapbox.ts index 198bfa4..387c86b 100644 --- a/src/mapbox.ts +++ b/src/mapbox.ts @@ -32,7 +32,7 @@ export class Mapbox { * @see {@link https://docs.mapbox.com/api/maps/styles/#retrieve-a-style} * @privateRemarks Token Scope - styles:read */ - @retry() + @retry static async getStyle(styleId: string, user = USERNAME): Promise { const res = await fetch(`${API}/styles/v1/${user}/${styleId}?access_token=${TOKEN}`); @@ -49,7 +49,7 @@ export class Mapbox { * @see {@link https://docs.mapbox.com/api/maps/styles/#list-styles} * @privateRemarks Token Scope - styles:list */ - @retry() + @retry static async getAllStyles() { const res = await fetch(`${API}/styles/v1/${USERNAME}?access_token=${TOKEN}&limit=${Date.now()}`); @@ -66,7 +66,7 @@ export class Mapbox { * @see {@link https://docs.mapbox.com/api/maps/styles/#delete-a-style} * @privateRemarks Token Scope - styles:write */ - @retry() + @retry static async deleteStyle(styleId: string) { const res = await fetch(`${API}/styles/v1/${USERNAME}/${styleId}?access_token=${TOKEN}`, { method: "DELETE" @@ -84,7 +84,7 @@ export class Mapbox { * @see {@link https://docs.mapbox.com/api/maps/styles/#create-a-style} * @privateRemarks Token Scope - styles:write */ - @retry() + @retry static async createStyle(styleObject: string) { const res = await fetch(`${API}/styles/v1/${USERNAME}?access_token=${TOKEN}`, { body: styleObject, @@ -109,7 +109,7 @@ export class Mapbox { * @see {@link https://docs.mapbox.com/api/maps/mapbox-tiling-service/#list-tilesets} * @privateRemarks Token Scope - tilesets:list */ - @retry() + @retry static async getAllTilesets(): Promise> { const res = await fetch(`${API}/tilesets/v1/${USERNAME}?limit=100&access_token=${TOKEN}`); @@ -126,7 +126,7 @@ export class Mapbox { * @see {@link https://docs.mapbox.com/api/maps/mapbox-tiling-service/#delete-tileset} * @privateRemarks Token Scope - tilesets:write */ - @retry() + @retry static async deleteTileset(tilesetId: string) { const res = await fetch(`${API}/tilesets/v1/${USERNAME}.${tilesetId}?access_token=${TOKEN}`, { method: "DELETE" @@ -144,7 +144,7 @@ export class Mapbox { * @see {@link https://docs.mapbox.com/api/maps/mapbox-tiling-service/#create-a-tileset} * @privateRemarks Token Scope - tilesets:write */ - @retry() + @retry static async createTileset(tilesetName: string, body: string) { const res = await fetch(`${API}/tilesets/v1/${USERNAME}.${tilesetName}?access_token=${TOKEN}`, { body, @@ -164,7 +164,7 @@ export class Mapbox { * @see {@link https://docs.mapbox.com/api/maps/mapbox-tiling-service/#publish-a-tileset} * @privateRemarks Token Scope - tilesets:write */ - @retry() + @retry static async publishTileset(tilesetName: string) { const res = await fetch(`${API}/tilesets/v1/${USERNAME}.${tilesetName}/publish?access_token=${TOKEN}`, { method: "POST" @@ -185,7 +185,7 @@ export class Mapbox { * @see {@link https://docs.mapbox.com/api/maps/mapbox-tiling-service/#retrieve-information-about-a-single-tileset-job} * @privateRemarks Token Scope - tilesets:read */ - @retry() + @retry static async getJobStatus(jobId: string, tilesetName: string) { const res = await fetch(`${API}/tilesets/v1/${USERNAME}.${tilesetName}/jobs/${jobId}?access_token=${TOKEN}`); @@ -207,7 +207,7 @@ export class Mapbox { * @see {@link https://docs.mapbox.com/api/maps/mapbox-tiling-service/#list-tileset-sources} * @privateRemarks Token Scope - tilesets:list */ - @retry() + @retry static async getAllTilesetSources() { const res = await fetch(`${API}/tilesets/v1/sources/${USERNAME}?limit=500&access_token=${TOKEN}`); @@ -225,7 +225,7 @@ export class Mapbox { * @see {@link https://docs.mapbox.com/api/maps/mapbox-tiling-service/#delete-a-tileset-source} * @privateRemarks Token Scope - tilesets:write */ - @retry() + @retry static async deleteTilesetSource(id: string) { const res = await fetch(`${API}/tilesets/v1/sources/${USERNAME}/${id}?access_token=${TOKEN}`, { method: "DELETE" @@ -244,7 +244,7 @@ export class Mapbox { * @see {@link https://docs.mapbox.com/api/maps/mapbox-tiling-service/#replace-a-tileset-source} * @privateRemarks Token Scope - tilesets:write */ - @retry() + @retry static async createTilesetSource(id: string, body: FormData) { const res = await fetch(`${API}/tilesets/v1/sources/${USERNAME}/${id}?access_token=${TOKEN}`, { body, @@ -267,7 +267,7 @@ export class Mapbox { * @see {@link https://docs.mapbox.com/api/maps/mapbox-tiling-service/#view-the-global-queue} * @privateRemarks Token Scope - tilesets:read */ - @retry() + @retry static async getQueue() { const res = await fetch(`${API}/tilesets/v1/queue?access_token=${TOKEN}`, { method: "PUT" @@ -291,7 +291,7 @@ export class Mapbox { * @see {@link https://docs.mapbox.com/api/maps/static-images/#retrieve-a-static-map-from-a-style} * @privateRemarks Token Scope - styles:tiles */ - @retry() + @retry static async getImage(styleId: string, size: ImageSize, position: string, padding?: number) { const url = `${API}/styles/v1/${USERNAME}/${styleId}/static/${position}${size.isZoom ? `,${size.zoom}` : ''}/${size.width / (size.twoX ? 2 : 1)}x${size.height / (size.twoX ? 2 : 1)}${size.twoX ? '@2x' : ''}?access_token=${TOKEN}${padding ? `&padding=${padding}` : ''}&logo=false&attribution=false&before_layer=${Date.now()}` console.log(url) @@ -308,40 +308,25 @@ export class Mapbox { } -/** - * Use this decorator to automatically retry failed API calls. - * @param numRetries - number of retry attempts - * @param delaySec - number of seconds to wait between retry attempts - */ -function retry(numRetries = 10, delaySec = 10) { - return function (_target: unknown, _key: string | symbol, descriptor: PropertyDescriptor) { - const original = descriptor.value - - if (typeof original === 'undefined') { - throw new Error('This should never happen: [descriptor value is undefined].') - } - - // deno-lint-ignore no-explicit-any - descriptor.value = async function (...args: any) { - do { - try { - return await original.apply(this, args); - } catch (error) { - --numRetries - console.log(`Mapbox API request threw an error.`) - console.log(error) - console.log(`Retrying in ${delaySec} seconds.`) - await delay(delaySec * 1000) - } - } while (numRetries >= 0); - - throw new Error('Mapbox API was unsuccessful after all retries.') - } - - return descriptor +/** Use this decorator to automatically retry failed API calls. */ +// deno-lint-ignore no-explicit-any +function retry(fn: any, _ctx: ClassMethodDecoratorContext) { + return async function (...args: unknown[]) { + let numRetries = 10 + const delaySec = 10 + + do { + try { + return await fn(...args) + } catch (error) { + --numRetries + console.log(`Mapbox API request threw an error.`) + console.log(error) + console.log(`Retrying in ${delaySec} seconds.`) + await delay(delaySec * 1000) + } + } while (numRetries >= 0); + + throw new Error('Mapbox API was unsuccessful after all retries.') } } - -if (import.meta.main) { - console.log(JSON.stringify(await Mapbox.getAllStyles())) -} -- GitLab From c4254f26d95b3de8052dbc149084a9e3c6b65c5a Mon Sep 17 00:00:00 2001 From: Travis Dillon Date: Wed, 14 Feb 2024 15:20:11 -0500 Subject: [PATCH 2/2] v5.10.6 --- README.md | 2 +- src/commands/version.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 868fa3a..13252b8 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ In order to use this application, follow these steps: --allow-net=api.mapbox.com,gitlab.com \ --name mapper \ --force \ - https://gitlab.com/api/v4/projects/tdillon%2fmapper/packages/generic/mapper/5.10.5/mapper.js + https://gitlab.com/api/v4/projects/tdillon%2fmapper/packages/generic/mapper/5.10.6/mapper.js ``` - Verify installation: diff --git a/src/commands/version.ts b/src/commands/version.ts index 9577151..6cbb9cc 100644 --- a/src/commands/version.ts +++ b/src/commands/version.ts @@ -1,4 +1,4 @@ -export const VERSION = '5.10.5' +export const VERSION = '5.10.6' if (import.meta.main) { console.log(VERSION) -- GitLab