Skip to content
This repository was archived by the owner on May 27, 2021. It is now read-only.

Commit e56a0ef

Browse files
[API-964] Ky rewrite (#133)
* wip * fuck fetch and the horse it rode in on * axios=>ky resources * Fix uglifyjs * Almost done * Remove axios tests * Disable test stage for now * File downloads * Fix header parsing * Use ky-universal * Cancelable promises * allow for passing custom signal * re-add methods * Fix sending http bodies * Add makeCancelable helper * typo * Add makeCancelable helper to some of the required places * Add makeCancelable helper everywhere * lint * docs * more docs * Odds and ends * Fix docs * make unobservable protected again * Remove deprecated code * Implement API-932 * Remove job monitor code * Rename Maps4News -> Mapcreator * Make ApiError use getters * Fix Choropleth::getJson * CancelablePromise docs * Fixes for docs * Use Class in jsdoc * Fixes for docs * ky docs * Fixes for docs * Fixes for docs * Fixes for docs * ApiError * Fix throw error syntax * Upgrade documentation * Formatting on tests * Fix most tests * enable tests * Fix last tests Co-authored-by: Kevin Richter <[email protected]>
1 parent 1a918d3 commit e56a0ef

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+1502
-2752
lines changed

.env.example

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
# Api host
22
HOST=https://api.maps4news.com
33

4-
# Default perPage value for listings
5-
PER_PAGE=12
6-
74
# Window options for the ImplicitFlowPopup
85
WINDOW_OPTIONS=width=800, height=600
96

107
# Show deleted items, possible values (none, only, all)
118
# Requires special api permissions
129
SHOW_DELETED=none
1310

11+
# Default perPage value for listings
12+
PER_PAGE=12
13+
1414
# JobMonitor max rows
1515
JOB_MONITOR_MAX_ROWS=100
1616

17-
# Cache
18-
CACHE_SHARED=true
19-
CACHE_SECONDS=1800
20-
CACHE_SHARED_AUTO_UPDATE=true
21-
CACHE_DEREFERENCE_OUTPUT=false
22-
2317
# debug, info, warn, error
2418
LOG_LEVEL=warn

.env.test

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
# Api host
22
HOST=https://api.maps4news.com
33

4-
# Default perPage value for listings
5-
PER_PAGE=12
6-
74
# Window options for the ImplicitFlowPopup
85
WINDOW_OPTIONS=width=800, height=600
96

107
# Show deleted items, possible values (none, only, all)
118
# Requires special api permissions
129
SHOW_DELETED=none
1310

11+
# Default perPage value for listings
12+
PER_PAGE=12
13+
1414
# JobMonitor max rows
1515
JOB_MONITOR_MAX_ROWS=100
1616

17-
# Cache
18-
CACHE_SHARED=false
19-
2017
# debug, info, warn, error
2118
LOG_LEVEL=debug

Jenkinsfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ node('npm') {
7676
PACKAGE_VERSION = sh(returnStdout: true, script: 'git describe --exact-match --tag HEAD 2>/dev/null || git rev-parse --short HEAD').trim()
7777
println "Published package version ${PACKAGE_VERSION}"
7878

79-
slackSend(color: 'good', message: "`@mapcreator/maps4news` version ${PACKAGE_VERSION} was just published, please run `npm install @mapcreator/maps4news${PACKAGE_VERSION.replace('v', '@')}`.", channel: '#api')
79+
slackSend(color: 'good', message: "`@mapcreator/api` version ${PACKAGE_VERSION} was just published, please run `npm install @mapcreator/api${PACKAGE_VERSION.replace('v', '@')}`.", channel: '#api')
8080
}
8181
}
8282

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
Maps4News Javascript API [![npm version](https://img.shields.io/npm/v/@mapcreator/maps4news.svg)](https://www.npmjs.com/package/@mapcreator/maps4news)
1+
Mapcreator Javascript API [![npm version](https://img.shields.io/npm/v/@mapcreator/api.svg)](https://www.npmjs.com/package/@mapcreator/api)
22
-------------------------
33

4-
The Maps4News API is a powerful mapping service built for our front-end applications. This library is released to
4+
The Mapcreator API is a powerful mapping service built for our front-end applications. This library is released to
55
provide a painless way of talking to the api using Javascript. See the [LICENSE] file for licensing information. Api
66
tokens can be granted through support requests.
77

UPGRADE.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Upgrading from @mapcreator/maps4news
2+
------------------------------------
3+
4+
The following changes are important to keep in mind when upgrading from
5+
`@mapcreator/maps4news` to `@mapcreator/api`.
6+
7+
- `JobMonitor`, `JobMonitorRow` and `JobMonitorFilter` classes have been removed
8+
- `PaginatedResourceWrapper` and `ResourceCache` classes have been removed
9+
- This also removes the `PaginatedResourceListing::wrap` and `SimpleResourceProxy::listAndWrap` methods
10+
- `Maps4News` class has been renamed to `Mapcreator`
11+
- Most methods that return Promises can now be canceled using the `CancelablePromise` type
12+
- `axios` has been deprecated in favor of `ky`. This means that calls like `api.axios.get()` need to be rewritten

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = {
1717
coverageDirectory: 'build/coverage',
1818
reporters: [
1919
'default',
20-
['jest-junit', {outputDirectory: 'build'}],
20+
['jest-junit', { outputDirectory: 'build' }],
2121
],
2222
setupFiles: [
2323
'<rootDir>/tests/setup.js',

manual/examples.authentication.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ Authentication is done through [OAuth]. This library provides multiple [OAuth fl
33
implementations for authentication. A client id can be obtained through a support
44
ticket but this is planned to change in the near future. The client will first
55
check if any tokens can be found in the cache before requiring authentication.
6-
If one can be found the `api.authenticate()` method will instantly resolve without
6+
If one can be found the `api.authenticate()` method will instantly resolve without
77
any side-effects. The variable `api.authenticated` will be set to true if a token
8-
has been found and is still valid.
8+
has been found and is still valid.
99

1010
Tokens are stored in HTTPS cookies if possible and using `localStorage` when the
1111
browser is not using a HTTPS connection. [Nodejs] uses a file named `.m4n_token` to store the token.
@@ -15,8 +15,8 @@ Multiple flows are supported for web browsers. All the web examples assume the w
1515
build of the library has been included in the target page.
1616

1717
### Implicit Flow
18-
A client id is required to use the implicit flow. The redirect url *must* be the
19-
same as the one linked to the client id. The callback url is automatically
18+
A client id is required to use the implicit flow. The redirect url *must* be the
19+
same as the one linked to the client id. The callback url is automatically
2020
guessed if none is provided.
2121

2222
```js
@@ -25,22 +25,22 @@ var clientId = 1;
2525

2626
// Callback url is set to the current url by default
2727
var auth = new ImplicitFlow(clientId);
28-
var api = new Maps4News(auth);
28+
var api = new Mapcreator(auth);
2929

3030
// This will hijack the page if no authentication cache can
3131
// be found. Smartest thing to do is to just let it happen
3232
// and initialize any other code afterwards.
3333
api.authenticate().then(function() {
3434
// Save the token
3535
api.saveToken();
36-
36+
3737
// Get the current user and dump the result to the console.
3838
api.users.get('me').then(console.dir);
3939
});
4040
```
4141

4242
### Implicit flow pop-up
43-
Just like the Implicit Flow a client id is required.
43+
Just like the Implicit Flow a client id is required.
4444

4545
```js
4646
// Obtained client id
@@ -53,23 +53,23 @@ var clientId = 1;
5353
// a custom page that just contains `api.authenticate()`
5454
// that uses ImplicitFlowPopup as the auth parameter.
5555
var auth = new ImplicitFlowPopup(clientId);
56-
var api = new Maps4News(auth);
56+
var api = new Mapcreator(auth);
5757

5858
// This will create a pop-up window containing the log in
5959
// page. Once the pop-up redirects back to the callback it
6060
// will resolve the promise. The callback page should contain
6161
api.authenticate().then(function() {
6262
// Save the token
6363
api.saveToken();
64-
64+
6565
// Get the current user and dump the result to the console.
6666
api.users.get('me').then(console.dir);
6767
});
6868
```
6969

7070
### Implicit flow pop-up (advanced)
7171
Due to the nature of the implicit flow pop-up (referred to as IFP from now on)
72-
method the callback page can be set to a blank page that just grabs the token
72+
method the callback page can be set to a blank page that just grabs the token
7373
and then closes. This can be done in the following way.
7474

7575
**index.html:**
@@ -79,13 +79,13 @@ var clientId = 1;
7979
var callbackUrl = 'https://example.com/callback.html';
8080

8181
var auth = new ImplicitFlowPopup(clientId);
82-
var api = new Maps4News(auth);
82+
var api = new Mapcreator(auth);
8383

8484
// This will resolve once the callback page has been loaded
8585
api.authenticate().then(function() {
8686
// Save the token
8787
api.saveToken();
88-
88+
8989
// Get the current user and dump the result to the console.
9090
api.users.get('me').then(console.dir);
9191
});
@@ -101,8 +101,8 @@ new ImplicitFlowPopup(clientId);
101101
```
102102

103103
### Password flow (dangerous)
104-
The password flow is **NOT** intended to be used in the browser. If you do
105-
decide to use the password flow then it is recommended to make sure that
104+
The password flow is **NOT** intended to be used in the browser. If you do
105+
decide to use the password flow then it is recommended to make sure that
106106
the site is **NOT** public facing and using HTTPS. Leaking the [secret] is
107107
a very bad idea.
108108

@@ -115,7 +115,7 @@ var password = 'Password1!'; // password
115115
// Secret will be leaked if this is used on a webpage. Please only use
116116
// this for non-web applications.
117117
var auth = new PasswordFlow(clientId, secret, username, password);
118-
var api = new Maps4News(auth);
118+
var api = new Mapcreator(auth);
119119

120120
// This will resolve once the authentication has completed
121121
api.authenticate().then(function() {
@@ -125,11 +125,11 @@ api.authenticate().then(function() {
125125
```
126126

127127
### Dummy flow
128-
The dummy flow can be used when a token *should* be present in the cache.
128+
The dummy flow can be used when a token *should* be present in the cache.
129129

130130
```js
131131
var auth = new DummyFlow();
132-
var api = new Maps4News(auth);
132+
var api = new Mapcreator(auth);
133133

134134
// Manually check if we're logged in
135135
if (api.authenticated) {
@@ -160,7 +160,7 @@ var username = '[email protected]'; // email is used for authentication
160160
var password = 'Password1!'; // password
161161

162162
var auth = new PasswordFlow(clientId, secret, username, password);
163-
var api = new Maps4News(auth);
163+
var api = new Mapcreator(auth);
164164

165165
// This will resolve once the authentication has completed
166166
api.authenticate().then(function() {
@@ -174,7 +174,7 @@ The dummy flow can also be used when a token is known.
174174

175175
```js
176176
var auth = new DummyFlow();
177-
var api = new Maps4News(auth);
177+
var api = new Mapcreator(auth);
178178

179179
var token = {
180180
token: "eyJ0eXAiOiJKV1...",

manual/examples.md

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# Basics
2-
These examples assume that an instance of the api exists and is authenticated.
2+
These examples assume that an instance of the api exists and is authenticated.
33
See the node and web authentication examples for more information on authenticating.
44

55
### Getting a resource
6-
Resources are bound to the base api class by default. Resources can be fetched in
6+
Resources are bound to the base api class by default. Resources can be fetched in
77
two ways; by selecting them (`.select`) or by fetching them (`.get`). Selecting them will only set the
88
object's id to it's properties. Fetching a resource
99

1010
Fetch resource and all it's properties:
1111

12-
```js
12+
```js
1313
api.colors.get(1).then(function(color) {
1414
console.log(color.id + " " + color.name + ": " + color.hex);
1515
})
@@ -25,7 +25,7 @@ api.users.select('me').mapstyleSets().then(function(sets) {
2525
});
2626
```
2727

28-
Selection is only usefull as a stepping stone to related resources that can be easily obtained
28+
Selection is only usefull as a stepping stone to related resources that can be easily obtained
2929
using the id of the parent. Please refer to the [api documentation] for further reference.
3030

3131
### Create a new resource
@@ -47,7 +47,7 @@ api.users.get('me').then(me => {
4747
```
4848

4949
### Clone a resource
50-
Setting the id to null forces the creation of a new object upon saving.
50+
Setting the id to null forces the creation of a new object upon saving.
5151

5252
```js
5353
api.colors.get(1).then(color => {
@@ -67,16 +67,16 @@ api.colors.list(1, 5).then(page => {
6767
console.log(page.data[i].toString());
6868
}
6969
});
70-
```
70+
```
7171

7272
Loop over every page and print the result to the console.
7373

7474
```js
7575
function parsePages(page) {
7676
for (var i = 0; i < page.data.length; i++) {
77-
console.log(page.data[i].toString());
78-
}
79-
77+
console.log(page.data[i].toString());
78+
}
79+
8080
if (page.hasNext) {
8181
console.log('Grabbing page ' + (page.page + 1));
8282
page.next().then(parsePage);
@@ -93,17 +93,17 @@ Loop over all pages and return the data in a promise
9393
```js
9494
function parsePages(page) {
9595
var data = [];
96-
96+
9797
function parse(page) {
9898
data = data.concat(page.data);
99-
99+
100100
if(page.hasNext) {
101101
return page.next().then(parse);
102102
} else {
103103
return data;
104104
}
105-
}
106-
105+
}
106+
107107
return parse(page);
108108
}
109109

@@ -133,7 +133,21 @@ var query = {
133133
api.layers.search(query).then(console.dir);
134134
```
135135

136-
The `search` method is an extension of `list`. This means that `.search({})` is the same as
136+
The `search` method is an extension of `list`. This means that `.search({})` is the same as
137137
`list()`. More information about search query formatting can be found in the [api documentation].
138-
139-
[api documepntation]: https://docs.maps4news.com/
138+
139+
### Canceling a requests
140+
Most methods that return a promise will have a method called `cancel`. This method can be called
141+
to cancel the request. If the request is running or about to be ran the promise will throw an error
142+
once canceled. If the request has been completed before the promise has been canceled it will not
143+
throw an error and instead complete successfully.
144+
145+
```js
146+
// Fetch a preview
147+
const promise = api.jobs.select(123456).downloadPreview();
148+
149+
// Turns out we don't need it anyways
150+
promise.cancel();
151+
```
152+
153+
[api documentation]: https://docs.maps4news.com/

manual/installation.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,30 @@ Installation can be done through either [npm] or [yarn].
33

44
```sh
55
// Using npm
6-
npm install --save @mapcreator/maps4news
6+
npm install --save @mapcreator/api
77

88
// Using yarn
9-
yarn add @mapcreator/maps4news
9+
yarn add @mapcreator/api
1010
```
1111

1212
After installation the package can be imported as follows:
1313

1414
```js
15-
var m4n = require('@mapcreator/maps4news');
15+
var m4n = require('@mapcreator/api');
1616

1717
// Do stuff
1818
var auth = new m4n.ImplicitFlow(1);
1919
var api = new m4n.Maps4News(auth);
2020
```
2121

2222
or using ES6 import statements:
23-
23+
2424
```js
25-
import {Maps4News, DummyFlow} from '@mapcreator/maps4news';
25+
import {Mapcreator, DummyFlow} from '@mapcreator/api';
2626

2727
// Do stuff
2828
var auth = new DummyFlow();
29-
var api = new Maps4News(auth);
29+
var api = new Mapcreator(auth);
3030
```
3131

3232
[npm]: https://npmjs.com

0 commit comments

Comments
 (0)