Skip to content

Commit 186aa38

Browse files
committed
feat: wait for db to be opened/change signature
1 parent 4eaebe5 commit 186aa38

9 files changed

Lines changed: 324 additions & 955 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
npm ci
2121
npm install -g codecov
2222
- name: Unit test
23-
run: npm t
23+
run: npm run coverage
2424
- name: Upload coverage
2525
uses: codecov/codecov-action@v6
2626
with:

README.md

Lines changed: 30 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
[![npm](https://img.shields.io/npm/v/pokeapi-js-wrapper)](https://www.npmjs.com/package/pokeapi-js-wrapper)
44
[![Tests](https://github.com/PokeAPI/pokeapi-js-wrapper/actions/workflows/test.yml/badge.svg)](https://github.com/PokeAPI/pokeapi-js-wrapper/actions/workflows/test.yml)
55
[![Mocha browser tests](https://img.shields.io/badge/test-browser-brightgreen.svg)](https://pokeapi.github.io/pokeapi-js-wrapper/test/test.html)
6-
[![codecov](https://codecov.io/gh/PokeAPI/pokeapi-js-wrapper/branch/master/graph/badge.svg)](https://codecov.io/gh/PokeAPI/pokeapi-js-wrapper)
6+
[![codecov](https://codecov.io/github/PokeAPI/pokeapi-js-wrapper/graph/badge.svg?token=4oXXOVxG2n)](https://codecov.io/github/PokeAPI/pokeapi-js-wrapper)
77

88
Maintainer: [Naramsim](https://github.com/Naramsim)
99

10-
A PokeAPI wrapper intended for browsers only. Comes fully asynchronous (with [localForage](https://github.com/localForage/localForage)) and built-in cache. Offers also Image Caching through the inclusion of a Service Worker. _For a Node (server-side) wrapper see: [pokedex-promise-v2](https://github.com/PokeAPI/pokedex-promise-v2)_
10+
A PokeAPI wrapper intended for browsers. Comes fully asynchronous, zero dependencies and built-in cache. Offers also Image Caching through the inclusion of a Service Worker. _For a Node (server-side) wrapper see: [pokedex-promise-v2](https://github.com/PokeAPI/pokedex-promise-v2)_
1111

1212
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
1313
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
@@ -25,26 +25,22 @@ A PokeAPI wrapper intended for browsers only. Comes fully asynchronous (with [lo
2525

2626
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
2727

28-
## Install
29-
30-
```sh
31-
npm install pokeapi-js-wrapper --save
32-
```
33-
34-
```html
35-
<script src="https://unpkg.com/pokeapi-js-wrapper/src/index.js"></script>
36-
```
37-
3828
## Usage
3929

4030
```js
41-
const Pokedex = require("pokeapi-js-wrapper")
42-
const P = new Pokedex()
31+
// As a Node module
32+
// npm install pokeapi-js-wrapper --save
33+
const pokedex = await Pokedex.init();
34+
console.log(await pokedex.getPokemonsList())
4335
```
4436

4537
```html
46-
<script>
47-
const P = new Pokedex()
38+
<!-- Included in some HTML -->
39+
<script type="module">
40+
import {Pokedex} from "https://cdn.jsdelivr.net/gh/pokeapi/pokeapi-js-wrapper@beta/src/index.js"
41+
const pokedex = await Pokedex.init();
42+
const version = await pokedex.getVersionByName(1)
43+
console.log(version)
4844
</script>
4945
```
5046

@@ -53,17 +49,17 @@ const P = new Pokedex()
5349
```js
5450
// with await, be sure to be in an async function (and in a try/catch)
5551
(async () => {
56-
const golduck = await P.getPokemonByName("golduck")
52+
const golduck = await pokedex.getPokemonByName("golduck")
5753
console.log(golduck)
5854
})()
5955

6056
// or with Promises
61-
P.getPokemonByName("eevee")
57+
pokedex.getPokemonByName("eevee")
6258
.then(function(response) {
6359
console.log(response)
6460
})
6561

66-
P.resource([
62+
pokedex.resource([
6763
"/api/v2/pokemon/36",
6864
"api/v2/berry/8",
6965
"https://pokeapi.co/api/v2/ability/9/",
@@ -74,11 +70,10 @@ P.resource([
7470

7571
## Configuration
7672

77-
Pass an Object to `Pokedex()` in order to configure it. Available options: `protocol`, `hostName`, `versionPath`, `cache`, `timeout`(ms), and `cacheImages`.
73+
Pass an Object to `Pokedex.init()` in order to configure it. Available options: `protocol`, `hostName`, `versionPath`, `cache`, `timeout`(ms), and `cacheImages`.
7874
Any option is optional :smile:. All the default values can be found [here](https://github.com/PokeAPI/pokeapi-js-wrapper/blob/master/src/config.js#L3-L10)
7975

8076
```js
81-
const Pokedex = require("pokeapi-js-wrapper")
8277
const customOptions = {
8378
protocol: "https",
8479
hostName: "localhost:443",
@@ -87,7 +82,7 @@ const customOptions = {
8782
timeout: 5 * 1000, // 5s
8883
cacheImages: true
8984
}
90-
const P = new Pokedex(customOptions)
85+
const pokedex = await Pokedex.init(customOptions);
9186
```
9287

9388
### Caching images
@@ -97,17 +92,17 @@ Pokeapi.co serves its Pokemon images through [Github](https://github.com/PokeAPI
9792
`pokeapi-js-wrapper` enables browsers to cache all these images by:
9893

9994
1. enabling the config parameter `cacheImages`
100-
2. serving [our service worker](https://raw.githubusercontent.com/PokeAPI/pokeapi-js-wrapper/master/dist/pokeapi-js-wrapper-sw.js) from the root of your project
95+
2. serving [our service worker](https://raw.githubusercontent.com/PokeAPI/pokeapi-js-wrapper/master/src/pokeapi-js-wrapper-sw.js) from the root of your project
10196

102-
In this way when `pokeapi-js-wrapper`'s `Pokedex` is created it will install and start the Service Worker you are serving at the root of your server. The Service Worker will intercept all the calls your HTML/CSS/JS are making to get PokeAPI's images and will cache them.
97+
In this way when `pokeapi-js-wrapper`'s `Pokedex` is initialized it will install and start the Service Worker you are serving at the root of your server. The Service Worker will intercept all the calls your HTML/CSS/JS are making to get PokeAPI's images and will cache them.
10398

10499
It's fundamental that you download the Service Worker [we provide](https://raw.githubusercontent.com/PokeAPI/pokeapi-js-wrapper/master/src/pokeapi-js-wrapper-sw.js)_(Right Click + Save As)_ and you serve it from the root of your project/server. Service Workers in fact cannot be installed from a domain different than yours.
105100

106101
A [basic example](https://github.com/PokeAPI/pokeapi-js-wrapper/blob/master/test/example-sw.html) is hosted [here](https://pokeapi.github.io/pokeapi-js-wrapper/test/example-sw.html).
107102

108103
## Tests
109104

110-
`pokeapi-js-wrapper` can be tested using two strategies. One is with Node, since this package works with Node (although not recommended), and the other with a browser.
105+
`pokeapi-js-wrapper` can be tested using two strategies. One is with Node, since this package works with Node, and the other with a browser.
111106

112107
```js
113108
npm test
@@ -120,23 +115,15 @@ Or open `/test/test.html` in your browser. A live version can be found at [`gh-p
120115
All the endpoints and the functions to access PokeAPI's endpoints are listed in the long table below. Each function `.ByName(name)` accepts names and ids. The only 5 functions `.ById(id)` only accept ids. You can also pass an array to each function, it will retrieve the data for each element asynchronously.
121116

122117
```js
123-
P.getPokemonByName("eevee").then(function(response) {
124-
console.log(response)
125-
})
118+
response = await pokedex.getPokemonByName("eevee")
126119

127-
P.getPokemonSpeciesByName(25).then(function(response) {
128-
console.log(response)
129-
})
120+
response = await pokedex.getPokemonSpeciesByName(25)
130121

131-
P.getBerryByName(["cheri", "chesto", 5]).then(function(response) {
132-
// `response` will be an Array containing 3 Objects
133-
// response.forEach((item) => {console.log(item.size)}) // 80,50,20
134-
console.log(response)
135-
})
122+
// `response` will be an Array containing 3 Objects
123+
// response.forEach((item) => {console.log(item.size)}) // 80,50,20
124+
response = await pokedex.getBerryByName(["cheri", "chesto", 5])
136125

137-
P.getMachineById(3).then(function(response) {
138-
console.log(response)
139-
})
126+
response = await pokedex.getMachineById(3)
140127
```
141128

142129
| Function | Mapped PokeAPI endpoint | Documentation |
@@ -207,42 +194,11 @@ const interval = {
207194
offset: 34,
208195
limit: 10,
209196
}
210-
P.getPokemonsList(interval).then(function(response) {
197+
pokedex.getPokemonsList(interval).then(function(response) {
211198
console.log(response)
212199
})
213200
```
214201

215-
<!--
216-
This is what you will get:
217-
218-
```json
219-
{
220-
"count": 1050,
221-
"next": "https://pokeapi.co/api/v2/pokemon/?offset=43&limit=10",
222-
"previous": "https://pokeapi.co/api/v2/pokemon/?offset=23&limit=10",
223-
"results": [
224-
{
225-
"name": "nidoking",
226-
"url": "https://pokeapi.co/api/v2/pokemon/34/"
227-
},
228-
{
229-
"name": "clefairy",
230-
"url": "https://pokeapi.co/api/v2/pokemon/35/"
231-
},
232-
// ...
233-
{
234-
"name": "golbat",
235-
"url": "https://pokeapi.co/api/v2/pokemon/42/"
236-
},
237-
{
238-
"name": "oddish",
239-
"url": "https://pokeapi.co/api/v2/pokemon/43/"
240-
}
241-
]
242-
}
243-
```
244-
-->
245-
246202
| Function | Mapped PokeAPI endpoint |
247203
| --- | --- |
248204
| `getEndpointsList()` | [/](https://pokeapi.co/api/v2/) |
@@ -267,6 +223,7 @@ This is what you will get:
267223
| `getItemFlingEffectsList()` | [/item-fling-effect](https://pokeapi.co/api/v2/item-fling-effect/) |
268224
| `getItemPocketsList()` | [/item-pocket](https://pokeapi.co/api/v2/item-pocket/) |
269225
| `getMachinesList()` | [/machine](https://pokeapi.co/api/v2/machine/) |
226+
| `getMeta()` | [/meta](https://pokeapi.co/api/v2/meta/) |
270227
| `getMovesList()` | [/move](https://pokeapi.co/api/v2/move/) |
271228
| `getMoveAilmentsList()` | [/move-ailment](https://pokeapi.co/api/v2/move-ailment/) |
272229
| `getMoveBattleStylesList()` | [/move-battle-style](https://pokeapi.co/api/v2/move-battle-style/) |
@@ -300,19 +257,15 @@ This is what you will get:
300257
Use `.resource()` to query any URL or path. Also this function accepts both single values and Arrays.
301258

302259
```js
303-
P.resource([
260+
pokedex.resource([
304261
"/api/v2/pokemon/36",
305262
"api/v2/berry/8",
306263
"https://pokeapi.co/api/v2/ability/9/",
307264
]).then(function(response) {
308265
console.log(response)
309266
})
310267

311-
P.resource("api/v2/berry/5").then(function(response) {
268+
pokedex.resource("api/v2/berry/5").then(function(response) {
312269
console.log(response)
313270
})
314271
```
315-
316-
## Internet Explorer 8
317-
318-
In order to target this browser you must load a `Promise` polyfill before `pokeapi-js-wrapper`. You can choose one of your choice, we recommend [jakearchibald/es6-promise](https://cdnjs.com/libraries/es6-promise) or [stefanpenner/es6-promise](https://github.com/stefanpenner/es6-promise)

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
],
1010
"scripts": {
1111
"doctoc": "doctoc .",
12-
"test": "node --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=lcov.info --test ./test/test.js",
12+
"test": "node --test ./test/test.js",
13+
"coverage": "node --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=lcov.info --test ./test/test.js",
1314
"serve": "http-server ."
1415
},
1516
"repository": {

src/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import { Config } from './config.js'
88
export class Pokedex {
99

1010
constructor(config) {
11-
this.config = new Config(config)
12-
openDB(this.config)
11+
this.config = config
12+
1313
// add to Pokedex.prototype all our endpoint functions
1414
endpoints.forEach(endpoint => {
1515
const endpointFullName = buildEndpointFullName(endpoint)
@@ -50,10 +50,16 @@ export class Pokedex {
5050
})
5151

5252
if (this.config.cacheImages) {
53-
import('./installSW.js').then(a=>a.installSW())
53+
import('./installSW.js').then(module=>module.installSW())
5454
}
5555
}
5656

57+
static async init(config) {
58+
config = new Config(config)
59+
await openDB(config)
60+
return new Pokedex(config)
61+
}
62+
5763
getConfig() {
5864
return this.config
5965
}

test/cdn.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
<html>
22
<body>
3-
<div>The first released Pokemon version name is: </div><div id="version"></div>
3+
<div>The first released Pokemon version name is: </div><div id="version"></div>
44
</body>
5+
<script>
6+
// var PJSW_DEBUG=1
7+
</script>
58
<script type="module">
69
import {Pokedex} from "https://cdn.jsdelivr.net/gh/pokeapi/pokeapi-js-wrapper@beta/src/index.js"
7-
const P = new Pokedex();
10+
const P = await Pokedex.init();
811
const version = await P.getVersionByName(1)
912
document.getElementById("version").textContent = version.name;
1013
</script>

test/example-sw.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<title>SW test</title>
55
<script type="module">
66
import {Pokedex} from '../src/index.js'
7-
const P = new Pokedex({ cacheImages: true });
7+
const P = await Pokedex.init({ cacheImages: true });
88
</script>
99
</head>
1010

test/test.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/10.2.0/mocha.min.css" integrity="sha512-K2SeadvFVe9lYFodeleJaoA3p2Kna1reHWkNJ3GWMint1X7cXdU7oJpe2TTkRZ65DCWp2kyxM8RNnEPstlWr4A==" crossorigin="anonymous" referrerpolicy="no-referrer" />
66
</head>
77
<body>
8+
<button id="flush-cache-btn">Flush Pokedex Cache</button>
89
<div id="mocha"></div>
910

1011
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/4.3.7/chai.min.js" integrity="sha512-Pwgr3yHn4Gvztp1GKl0ihhAWLZfqgp4/SbMt4HKW7AymuTQODMCNPE7v1uGapTeOoQQ5Hoz367b4seKpx6j7Zg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
1112
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/10.2.0/mocha.min.js" integrity="sha512-jsP/sG70bnt0xNVJt+k9NxQqGYvRrLzWhI+46SSf7oNJeCwdzZlBvoyrAN0zhtVyolGcHNh/9fEgZppG2pH+eA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
1213
<script>
13-
//var PJSW_DEBUG=1
14+
var PJSW_DEBUG=1
1415
</script>
1516
<script type="module" src="test.html.js"></script>
1617

0 commit comments

Comments
 (0)