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

Commit 9e25c74

Browse files
Mechazawabeschoenen
authored andcommitted
wrapper docs (#2)
* docs * Update wrapper.html.md * Update wrapper.html.md
1 parent 68e0f3e commit 9e25c74

File tree

1 file changed

+61
-113
lines changed

1 file changed

+61
-113
lines changed

pages/wrapper.html.md

Lines changed: 61 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@ search: true
1616
```
1717
// Using npm
1818
npm install --save @mapcreator/maps4news
19-
20-
// Using yarn
21-
yarn add @mapcreator/maps4news
2219
```
2320

24-
Installation can be done through either npm or yarn.
21+
Installation can be done either through a node package manager, such as npm or yarn, or by including the browser bundle.
2522

2623
## NodeJS
2724

@@ -50,7 +47,7 @@ Or when using ES6 import statements
5047
## Browser Script Tag
5148

5249
```html
53-
<script src="https://unpkg.com/@mapcreator/maps4news@1.4.22/dist/bundle.browser.min.js"></script>
50+
<script src="https://unpkg.com/@mapcreator/maps4news/dist/bundle.browser.min.js"></script>
5451
```
5552

5653
> This html tag can be used without any other dependency in your html.
@@ -83,6 +80,14 @@ browser is not using a HTTPS connection. NodeJS uses a file named `.m4n_token` t
8380
Multiple flows are supported for web browsers. All the web examples assume the web
8481
build of the library has been included in the target page.
8582

83+
## Machine token
84+
```js
85+
const token = "...";
86+
const api = new Maps4News(token);
87+
```
88+
89+
A machine token can be used directly while instaciating the api instance.
90+
8691
## Implicit flow
8792

8893
```js
@@ -111,35 +116,6 @@ guessed if none is provided.
111116

112117
## Implicit flow pop-up
113118

114-
```js
115-
// Obtained client id
116-
var clientId = 1;
117-
118-
// Callback url is set to the current url by default. The
119-
// script is smart enough close the page if it detects that
120-
// it's a child after authentication. This means that either
121-
// the current page can be set as the callback (default) or
122-
// a custom page that just contains `api.authenticate()`
123-
// that uses ImplicitFlowPopup as the auth parameter.
124-
var auth = new ImplicitFlowPopup(clientId);
125-
var api = new Maps4News(auth);
126-
127-
// This will create a pop-up window containing the log in
128-
// page. Once the pop-up redirects back to the callback it
129-
// will resolve the promise. The callback page should contain
130-
api.authenticate().then(function() {
131-
// Save the token
132-
api.saveToken();
133-
134-
// Get the current user and dump the result to the console.
135-
api.users.get('me').then(console.dir);
136-
});
137-
```
138-
139-
Just like the Implicit Flow a client id is required.
140-
141-
## Implicit flow pop-up (advanced)
142-
143119
> index.html
144120
145121
```js
@@ -149,7 +125,6 @@ var callbackUrl = 'https://example.com/callback.html';
149125
var auth = new ImplicitFlowPopup(clientId);
150126
var api = new Maps4News(auth);
151127

152-
// This will resolve once the callback page has been loaded
153128
api.authenticate().then(function() {
154129
// Save the token
155130
api.saveToken();
@@ -161,41 +136,16 @@ api.authenticate().then(function() {
161136

162137
> callback.html
163138
164-
```js
165-
var clientId = 1;
166-
167-
// This will instantly detect the token and close the page
168-
new ImplicitFlowPopup(clientId);
139+
```html
140+
<html><body>
141+
<h1>Nothing to see here 👻</h1>
142+
</body></html>
169143
```
170144

171-
Due to the nature of the implicit flow pop-up (referred to as IFP from now on)
172-
method the callback page can be set to a blank page that just grabs the token
173-
and then closes. This can be done in the following way.
174145

175-
## Password flow (dangerous)
146+
This will create a pop-up window containing the login page. Once the pop-up redirects back to the callback it will resolve the promise. The callback can be an empty page hosted on the same domain.
176147

177-
```js
178-
var clientId = 1; // client id
179-
var secret = ''; // secret
180-
var username = '[email protected]'; // email is used for authentication
181-
var password = 'Password1!'; // password
182-
183-
// Secret will be leaked if this is used on a webpage. Please only use
184-
// this for non-web applications.
185-
var auth = new PasswordFlow(clientId, secret, username, password);
186-
var api = new Maps4News(auth);
187-
188-
// This will resolve once the authentication has completed
189-
api.authenticate().then(function() {
190-
// Get the current user and dump the result to the console.
191-
api.users.get('me').then(console.dir);
192-
});
193-
```
194-
195-
The password flow is **NOT** intended to be used in the browser. If you do
196-
decide to use the password flow then it is recommended to make sure that
197-
the site is **NOT** public facing and using HTTPS. Leaking the secret is
198-
a very bad idea.
148+
Callback url is set to the current url by default. The script is smart enough close the page if it detects that it's a child after authentication. This means that either the current page can be set as the callback (default) or a blank page. The callback must be hosted on the same domain as the application to allow for cross window communication.
199149

200150
## Dummy flow
201151

@@ -219,65 +169,39 @@ api.authenticate().then(function() {
219169

220170
The dummy flow can be used when a token *should* be present in the cache.
221171

222-
# Authentication NodeJS
223-
The library currently only supports the password flow and the dummy flow
224-
for nodeJS. Other flows might be added in the future.
225-
226-
## Password Flow
172+
# Basics
227173

228174
```js
229-
var clientId = 1; // client id
230-
var secret = ''; // secret
231-
var username = '[email protected]'; // email is used for authentication
232-
var password = 'Password1!'; // password
175+
const me = await api.users.get('me');
176+
const colors = await me.colors.list();
177+
```
233178

234-
var auth = new PasswordFlow(clientId, secret, username, password);
235-
var api = new Maps4News(auth);
179+
> Which is the same as
236180
237-
// This will resolve once the authentication has completed
238-
api.authenticate().then(function() {
239-
// Get the current user and dump the result to the console.
240-
api.users.get('me').then(console.dir);
241-
});
181+
```js
182+
const colors = await api.users.select('me').colors.list();
242183
```
243184

244-
Make sure to store your secret somewhere safe and to only store the token
245-
and **never** the unencrypted user password.
185+
These examples assume that an instance of the api exists and is authenticated.
186+
See the node and web authentication examples for more information on authenticating.
246187

247-
## Dummy flow
188+
The wrapper exposes relations which return proxies. These proxies can be used to either build a route to a resource or to fetch resources. This means that `api.users.get('me')` is the same as calling the route `/v1/users/me`. All proxies expose the methods `new`, `list` and `lister`. Most proxies expose the methods `select` and `get`.
248189

249190
```js
250-
var auth = new DummyFlow();
251-
var api = new Maps4News(auth);
252-
253-
var token = {
254-
token: "eyJ0eXAiOiJKV1...",
255-
type: "Bearer",
256-
expires: "Thu, 18 May 2017 14:14:38 GMT"
191+
// Case translation
192+
const data = {
193+
foo_bar_baz: 123
257194
};
258195

259-
// Set the token
260-
api.auth.token = OAuthToken.fromResponseObject(token);
261-
262-
// Manually check if we're logged in
263-
if (api.authenticated) {
264-
console.log('Found authentication token in cache!');
265-
}
196+
const test = api.static().new(data);
266197

267-
api.authenticate().then(function() {
268-
// Will only resolve if a token was found
269-
console.log("We're authenticated");
270-
}).catch(function(err) {
271-
// This will be called if `api.authenticated` is false
272-
console.log(err.toString());
273-
});
198+
test.fooBarBaz === 123;
274199
```
275200

276-
The dummy flow can also be used when a token is known.
201+
The wrapper will transform snake_case named variables returned from the api into camelCase named variables. This means that for example `place_name` will be transformed into `placeName`.
202+
203+
Async methods return a `Promise` this means that both `then/catch` and `await/async` syntax are supported.
277204

278-
# Basics
279-
These examples assume that an instance of the api exists and is authenticated.
280-
See the node and web authentication examples for more information on authenticating.
281205

282206
## Getting a resource
283207

@@ -292,9 +216,9 @@ api.colors.get(1).then(function(color) {
292216
> Select the current user to quickly obtain related mapstyle sets
293217
294218
```js
295-
api.users.select('me').mapstyleSets().then(function(sets) {
296-
for(var i = 0; i < sets.data.length; i++) {
297-
console.log(sets.data[i].name);
219+
api.users.select('me').mapstyleSets.list().then(function(sets) {
220+
for (const set of sets.data) {
221+
console.log(`[${set.id}] ${set.name}`);
298222
}
299223
});
300224
```
@@ -320,6 +244,7 @@ Create a new color and dump the new resource to the console after saving
320244
```js
321245
api.users.get('me').then(me => {
322246
me.profession = 'Developer';
247+
323248
me.save(); // Optional chaining to get the updated resource
324249
});
325250
```
@@ -337,6 +262,27 @@ api.colors.get(1).then(color => {
337262

338263
Setting the id to null forces the creation of a new object upon saving.
339264

265+
## Creating a new job and revision
266+
267+
```js
268+
const object = {}; // Should contain the map definition
269+
270+
const job = await api.jobs.new({
271+
title: "api map",
272+
jobTypeId: 1
273+
}).save();
274+
275+
const revision = await job.revisions.new({
276+
mapstyleSetId: 1
277+
}).save(object);
278+
279+
// Will resolve when the request finishes. Not when the build is done.
280+
await revision.build("https://example.com/callback");
281+
```
282+
283+
Creating a new job and building it is pretty straight foreward. Revisions are slightly different then other resource instances. Their save function requires the new map definition as an argument. This is to make it easier to re-use the same revision instance.
284+
285+
340286
## Pagination
341287

342288
> Listing resources with pagination. First page with 5 items per page
@@ -403,6 +349,8 @@ api.users.select('me').colors.list().then(page => {
403349
});
404350
```
405351

352+
**warning**: The paginatedResourceListing is in the progress of being deprecated.
353+
406354
## Searching
407355

408356
> Resource lists can be queried to search for specific records as follows

0 commit comments

Comments
 (0)