From 1d4be997ca3dcfd6a01aa839f74c29ab95da177a Mon Sep 17 00:00:00 2001 From: Michiel van der Velde Date: Fri, 26 Mar 2021 01:23:45 +0100 Subject: [PATCH] chore(release): v0.0.1 --- .editorconfig | 15 ++++++++++ .gitattributes | 17 +++++++++++ .gitignore | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++ LICENSE | 21 ++++++++++++++ README.md | 24 ++++++++++++++++ package.json | 23 +++++++++++++++ src/index.ts | 54 ++++++++++++++++++++++++++++++++++ tsconfig.json | 22 ++++++++++++++ 8 files changed, 254 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 package.json create mode 100644 src/index.ts create mode 100644 tsconfig.json diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..5fd4d65 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.{json,yml}] +indent_size = 2 + +[*.md] +trim_trailing_whitespace = false diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..bdb0cab --- /dev/null +++ b/.gitattributes @@ -0,0 +1,17 @@ +# Auto detect text files and perform LF normalization +* text=auto + +# Custom for Visual Studio +*.cs diff=csharp + +# Standard to msysgit +*.doc diff=astextplain +*.DOC diff=astextplain +*.docx diff=astextplain +*.DOCX diff=astextplain +*.dot diff=astextplain +*.DOT diff=astextplain +*.pdf diff=astextplain +*.PDF diff=astextplain +*.rtf diff=astextplain +*.RTF diff=astextplain diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1700415 --- /dev/null +++ b/.gitignore @@ -0,0 +1,78 @@ +# Project-specific +dist + +# Logs +logs +*.log + +# Runtime data +pids +*.pid +*.seed + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directory +# Commenting this out is preferred by some people, see +# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- +node_modules + +# Users Environment Variables +.lock-wscript + +# ========================= +# Operating System Files +# ========================= + +# OSX +# ========================= + +.DS_Store +.AppleDouble +.LSOverride + +# Thumbnails +._* + +# Files that might appear on external disk +.Spotlight-V100 +.Trashes + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# Windows +# ========================= + +# Windows image file caches +Thumbs.db +ehthumbs.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msm +*.msp + +# Windows shortcuts +*.lnk diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..92def08 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2020-2021 Michiel van der Velde + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..0d0b34a --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +# ESI-Browser + +Small module to make unauthenticated requests to Eve Online's ESI API +from the browser. + +* Stores the retrieved result in `localStorage` +* Respects the `Expires` and `ETag` headers + +## Install + +``` +npm i @art-of-coding/esi-browser +``` + +## Usage + +To request information about a type: + +```ts +const typeId = 36 +const data = await fetchFromESI(`/universe/types/${typeId}`) + +console.log(`This item is called ${data.name}`) +``` diff --git a/package.json b/package.json new file mode 100644 index 0000000..5939853 --- /dev/null +++ b/package.json @@ -0,0 +1,23 @@ +{ + "name": "@art-of-coding/esi-browser", + "version": "0.0.1", + "description": "Small module to make unauthented request to Eve Online ESI", + "main": "./dist/index.js", + "author": "Michiel van der Velde ", + "homepage": "https://github.com/Art-of-Coding/esi-browser", + "repository": { + "type": "git", + "url": "git+https://github.com/Art-of-Coding/esi-browser.git" + }, + "files": [ + "dist" + ], + "keywords": [ + "eve", + "esi" + ], + "license": "MIT", + "devDependencies": {}, + "dependencies": { + } +} diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..06880c4 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,54 @@ +/** URL to Eve Online ESI */ +export const ESI_URL = 'https://esi.evetech.net/latest' + +export interface ESIObject { + ETag: string, + expires: number, + data: T +} + +export default async function fetchFromESI (uri: string, endpoint = ESI_URL) { + const key = `/esi${uri}` + const strValue = localStorage.getItem(key) + + let info: ESIObject = null + + if (strValue) { + try { + info = JSON.parse(strValue) + } catch (e) {} + } + + // + const headers: Record = {} + + if (info) { + if (info.expires > Date.now()) { + // Not expired - return data + return info.data + } + + headers['If-None-Match'] = info.ETag + } + + const response = await fetch(`${endpoint}${uri}`, { headers }) + + if (info && response.status === 304) { + info.expires = new Date(response.headers.get('expires') ?? Date.now()).getTime() + + // Store the object again + localStorage.setItem(key, JSON.stringify(info)) + + return info.data + } + + info = { + ETag: response.headers.get('etag') ?? '', + expires: new Date(response.headers.get('expires') ?? Date.now()).getTime(), + data: await response.json() + } + + // Store the object + localStorage.setItem(key, JSON.stringify(info)) + return info.data +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..52c29f1 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "target": "ESNext", + "module": "commonjs", + "moduleResolution": "node", + "removeComments": true, + "preserveConstEnums": true, + "experimentalDecorators": true, + "emitDecoratorMetadata": true, + "esModuleInterop": true, + "noImplicitAny": true, + "declaration": true, + "outDir": "./dist" + }, + "include": [ + "./src/**/*" + ], + "exclude": [ + "node_modules", + "dist" + ] +}