Skip to content

Commit

Permalink
chore(release): v0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
MichielvdVelde committed Mar 26, 2021
0 parents commit 1d4be99
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -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
78 changes: 78 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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}`)
```
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <michiel@michielvdvelde.nl>",
"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": {
}
}
54 changes: 54 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/** URL to Eve Online ESI */
export const ESI_URL = 'https://esi.evetech.net/latest'

export interface ESIObject<T = any> {
ETag: string,
expires: number,
data: T
}

export default async function fetchFromESI<T = any> (uri: string, endpoint = ESI_URL) {
const key = `/esi${uri}`
const strValue = localStorage.getItem(key)

let info: ESIObject<T> = null

if (strValue) {
try {
info = JSON.parse(strValue)
} catch (e) {}
}

//
const headers: Record<string, string> = {}

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') ?? '<missing>',
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
}
22 changes: 22 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"
]
}

0 comments on commit 1d4be99

Please sign in to comment.