@16patsle/pokeapi.js 中文文档教程
pokeapi.js
(v2) 的简单 JavaScript 包装器,带有简单的内置缓存
General
PokéAPI ://phalt.co">保罗哈利特。 它是用 JavaScript 编写的,并使用了现代技术,如 fetch 和 promises。
Install
pokeapi.js v1.1 提供 UMD 和 ES 模块格式。
ES modules
如果你在你的项目中使用 ES 模块(例如通过 Webpack 或 Rollup),ES 模块版本(dist/pokeapi.esm.js
)应该由你的模块打包器自动选择。 只需从 npm 安装它:
npm install @16patsle/pokeapi.js
然后您只需正常导入它:
import PokeApi from '@16patsle/pokeapi.js';
With a script tag (browser global)
就像您期望的那样:
<script src="path/to/pokeapi.js"></script>
PokeApi
现在是一个全局变量。
如果将 pokeapi.js.map
放在同一个文件夹中,您将在浏览器开发工具中获得更好的调试体验。
CommonJS or another module system
只需像往常一样导入库。 例如对于 Commonjs:
const PokeApi = require('@16patsle/pokeapi.js')
Usage
您可以使用变量 PokeApi
(如果您使用的是模块加载器,则可以选择命名它的任何名称)访问该库中的方法。 可以在此对象上访问所有方法。
Example
此代码将获取 ID 为 573 的 Pokémon Cinccino 的数据,并将名称记录到控制台。
PokeApi.getPokemon(573).then(function(response){
console.log(response.name) // "cinccino"
});
API Docs
可以在 API 网站 上找到 API 返回数据的文档。 方法名称遵循与 API 端点相同的命名方案,并且结构相同。
Building
要构建项目,首先运行 npm install
。 然后,使用 npm run build
构建文件。
Testing
这个库使用 Testem 和 Jasmine供测试用。 在 CI 模式下运行测试套件:
npm test
开发
npm run test:dev
Compatibility
中:如果您担心对旧浏览器的支持,请查看兼容性表。 要在不支持 localStorage 的浏览器中支持自动缓存,请在 src/localStotage.js
中包含 polyfill。
Compatibility | pokeapi.js | pokeapi.js + fetch polyfill | pokeapi.js + fetch polyfill + es6-promise polyfill |
---|---|---|---|
Firefox | 43+ | 29+ | 1+ |
Chrome | 42+ | 33+ | 1+ |
Opera | 35+ | 35+ | 11+ (at least) |
IE | ? | 10+ | 7+ |
Edge | ? | 13+ | 11+ |
Safari | ? | 9+ | 1.2+ |
Chrome for Android | 49+ | 49+ | 1+ |
Android Browser | 47+ | 4.4.4+ | 4.4.4+ (at least) |
iOS Safari | ? | 8.4+ | 8.4+ (at least) |
pokeapi.js
A simple JavaScript wrapper for the PokéAPI (v2), with simple built-in caching
General
This is a simple wraper for the PokéAPI by Paul Hallett. It is written in JavaScript and uses modern technologies like fetch and promises.
Install
pokeapi.js v1.1 is available both in UMD and ES module format.
ES modules
If you are using ES modules in your project (through Webpack or Rollup for example), the ES module version (dist/pokeapi.esm.js
) should be chosen automatically by your module bundler. Just install it from npm:
npm install @16patsle/pokeapi.js
Then you just import it normally:
import PokeApi from '@16patsle/pokeapi.js';
With a script tag (browser global)
Like you would expect:
<script src="path/to/pokeapi.js"></script>
PokeApi
is now a global variable.
If you place pokeapi.js.map
in the same folder, you will get a better debug experience in the browser dev tools.
CommonJS or another module system
Just import the library like you would normally. For example for Commonjs:
const PokeApi = require('@16patsle/pokeapi.js')
Usage
You access the methods from this library with the variable PokeApi
(or whatever you chose to name it if you are using a module loader). All methods can be accessed on this object.
Example
This code will fetch the data for Cinccino, the Pokémon with id 573, and log the name to the console.
PokeApi.getPokemon(573).then(function(response){
console.log(response.name) // "cinccino"
});
API Docs
Docs for the data the api returns can be found on the API's website. The method names follow the same naming scheme as the API endpoints and is structured the same way.
Building
To build the project, first run npm install
. Then, use npm run build
to build the files.
Testing
This library uses Testem along with Jasmine for testing. To run the test suite in CI mode:
npm test
In development:
npm run test:dev
Compatibility
Here is the compatibility table in case you are concerned about support for ancient browsers. To support auto-caching in browsers without support for localStorage, include the polyfill in src/localStotage.js
.
Compatibility | pokeapi.js | pokeapi.js + fetch polyfill | pokeapi.js + fetch polyfill + es6-promise polyfill |
---|---|---|---|
Firefox | 43+ | 29+ | 1+ |
Chrome | 42+ | 33+ | 1+ |
Opera | 35+ | 35+ | 11+ (at least) |
IE | ? | 10+ | 7+ |
Edge | ? | 13+ | 11+ |
Safari | ? | 9+ | 1.2+ |
Chrome for Android | 49+ | 49+ | 1+ |
Android Browser | 47+ | 4.4.4+ | 4.4.4+ (at least) |
iOS Safari | ? | 8.4+ | 8.4+ (at least) |
你可能也喜欢
- 1kb-anchor-scroll 中文文档教程
- @0soft/zero-lib 中文文档教程
- @0xc/serverless-config-transformer 中文文档教程
- @100shapes/theme-tailwind 中文文档教程
- @10play/error-boundary 中文文档教程
- @1amageek/firestore-commerce 中文文档教程
- @2bit/osc 中文文档教程
- @360bid/ebid-svga 中文文档教程
- @3wks/generator-gae-node-nestjs 中文文档教程
- @4geit/rct-text-selector-component 中文文档教程