Skip to content

Commit 88dd9f0

Browse files
authored
Merge pull request #2 from mrstegeman/rework
Add an example API handler.
2 parents 1fb7435 + 56d88c8 commit 88dd9f0

File tree

12 files changed

+1562
-10
lines changed

12 files changed

+1562
-10
lines changed

.eslintrc.js

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
module.exports = {
2+
'env': {
3+
'browser': true,
4+
'commonjs': true,
5+
'es6': true,
6+
'jasmine': true,
7+
'jest': true,
8+
'mocha': true,
9+
'node': true
10+
},
11+
'extends': 'eslint:recommended',
12+
'parser': 'babel-eslint',
13+
'parserOptions': {
14+
'sourceType': 'module'
15+
},
16+
'rules': {
17+
'arrow-parens': [
18+
'error',
19+
'always'
20+
],
21+
'arrow-spacing': 'error',
22+
'block-scoped-var': 'error',
23+
'block-spacing': [
24+
'error',
25+
'always'
26+
],
27+
'brace-style': [
28+
'error',
29+
'1tbs'
30+
],
31+
'comma-dangle': [
32+
'error',
33+
'always-multiline'
34+
],
35+
'comma-spacing': 'error',
36+
'comma-style': [
37+
'error',
38+
'last'
39+
],
40+
'computed-property-spacing': [
41+
'error',
42+
'never'
43+
],
44+
'curly': 'error',
45+
'dot-notation': 'error',
46+
'eol-last': 'error',
47+
'func-call-spacing': [
48+
'error',
49+
'never'
50+
],
51+
'implicit-arrow-linebreak': [
52+
'error',
53+
'beside'
54+
],
55+
'indent': [
56+
'error',
57+
2,
58+
{
59+
'ArrayExpression': 'first',
60+
'CallExpression': {
61+
'arguments': 'first'
62+
},
63+
'FunctionDeclaration': {
64+
'parameters': 'first'
65+
},
66+
'FunctionExpression': {
67+
'parameters': 'first'
68+
},
69+
'ObjectExpression': 'first',
70+
'SwitchCase': 1
71+
}
72+
],
73+
'key-spacing': [
74+
'error',
75+
{
76+
'afterColon': true,
77+
'beforeColon': false,
78+
'mode': 'strict'
79+
}
80+
],
81+
'keyword-spacing': [
82+
'error',
83+
{
84+
'after': true,
85+
'before': true
86+
}
87+
],
88+
'linebreak-style': [
89+
'error',
90+
'unix'
91+
],
92+
'lines-between-class-members': [
93+
'error',
94+
'always'
95+
],
96+
'max-len': [
97+
'error',
98+
80
99+
],
100+
'multiline-ternary': [
101+
'error',
102+
'always-multiline'
103+
],
104+
'no-console': 0,
105+
'no-duplicate-imports': 'error',
106+
'no-eval': 'error',
107+
'no-floating-decimal': 'error',
108+
'no-implicit-globals': 'error',
109+
'no-implied-eval': 'error',
110+
'no-lonely-if': 'error',
111+
'no-multi-spaces': [
112+
'error',
113+
{
114+
'ignoreEOLComments': true
115+
}
116+
],
117+
'no-multiple-empty-lines': 'error',
118+
'no-prototype-builtins': 'off',
119+
'no-return-assign': 'error',
120+
'no-script-url': 'error',
121+
'no-self-compare': 'error',
122+
'no-sequences': 'error',
123+
'no-shadow-restricted-names': 'error',
124+
'no-tabs': 'error',
125+
'no-trailing-spaces': 'error',
126+
'no-undefined': 'error',
127+
'no-unmodified-loop-condition': 'error',
128+
'no-unused-vars': [
129+
'error',
130+
{
131+
'argsIgnorePattern': '^_',
132+
'varsIgnorePattern': '^_'
133+
}
134+
],
135+
'no-useless-computed-key': 'error',
136+
'no-useless-concat': 'error',
137+
'no-useless-constructor': 'error',
138+
'no-useless-return': 'error',
139+
'no-var': 'error',
140+
'no-void': 'error',
141+
'no-whitespace-before-property': 'error',
142+
'object-curly-newline': [
143+
'error',
144+
{
145+
'consistent': true
146+
}
147+
],
148+
'object-curly-spacing': [
149+
'error',
150+
'never'
151+
],
152+
'object-property-newline': [
153+
'error',
154+
{
155+
'allowMultiplePropertiesPerLine': true
156+
}
157+
],
158+
'operator-linebreak': [
159+
'error',
160+
'after'
161+
],
162+
'padded-blocks': [
163+
'error',
164+
{
165+
'blocks': 'never'
166+
}
167+
],
168+
'prefer-const': 'error',
169+
'prefer-template': 'error',
170+
'quote-props': [
171+
'error',
172+
'as-needed'
173+
],
174+
'quotes': [
175+
'error',
176+
'single',
177+
{
178+
'allowTemplateLiterals': true
179+
}
180+
],
181+
'semi': [
182+
'error',
183+
'always'
184+
],
185+
'semi-spacing': [
186+
'error',
187+
{
188+
'after': true,
189+
'before': false
190+
}
191+
],
192+
'semi-style': [
193+
'error',
194+
'last'
195+
],
196+
'space-before-blocks': [
197+
'error',
198+
'always'
199+
],
200+
'space-before-function-paren': [
201+
'error',
202+
{
203+
'anonymous': 'never',
204+
'asyncArrow': 'always',
205+
'named': 'never'
206+
}
207+
],
208+
'space-in-parens': [
209+
'error',
210+
'never'
211+
],
212+
'space-infix-ops': 'error',
213+
'space-unary-ops': [
214+
'error',
215+
{
216+
'nonwords': false,
217+
'words': true
218+
}
219+
],
220+
'spaced-comment': [
221+
'error',
222+
'always',
223+
{
224+
'block': {
225+
'balanced': true,
226+
'exceptions': [
227+
'*'
228+
]
229+
}
230+
}
231+
],
232+
'switch-colon-spacing': [
233+
'error',
234+
{
235+
'after': true,
236+
'before': false
237+
}
238+
],
239+
'template-curly-spacing': [
240+
'error',
241+
'never'
242+
],
243+
'yoda': 'error'
244+
}
245+
};

.github/workflows/nodeapplication.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Node.js application
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- master
7+
push:
8+
branches:
9+
- master
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
strategy:
15+
matrix:
16+
node_version: [8, 10, 12]
17+
steps:
18+
- uses: actions/checkout@v1
19+
- uses: actions/setup-node@v1
20+
with:
21+
node-version: ${{ matrix.node_version }}
22+
- name: Install dependencies
23+
run: |
24+
npm install
25+
- name: Lint with eslint
26+
run: |
27+
npm run lint

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1+
*.swp
12
*.tgz
3+
*~
4+
/node_modules/
25
/package/
6+
SHA256SUMS

css/extension.css

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,17 @@
1515
top: calc(50% - 7.6rem);;
1616
transform: translateY(-50%);
1717
max-height: 100%;
18-
text-align: center;
18+
text-align: left;
1919
font-size: 1.6rem;
2020
color: #fff;
21+
max-width: 60rem;
22+
margin: 0 auto;
23+
}
24+
25+
#extension-example-extension-form-submit {
26+
font-size: 1.6rem;
27+
padding: 0.5rem 1rem;
28+
border: none;
29+
border-radius: 0.5rem;
30+
margin: 1rem 0;
2131
}

example-api-handler.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
const {APIHandler, APIResponse} = require('gateway-addon');
4+
const manifest = require('./manifest.json');
5+
6+
/**
7+
* Example API handler.
8+
*/
9+
class ExampleAPIHandler extends APIHandler {
10+
constructor(addonManager) {
11+
super(addonManager, manifest.id);
12+
addonManager.addAPIHandler(this);
13+
}
14+
15+
async handleRequest(request) {
16+
if (request.method !== 'POST' || request.path !== '/example-api') {
17+
return new APIResponse({status: 404});
18+
}
19+
20+
// echo back the body
21+
return new APIResponse({
22+
status: 200,
23+
contentType: 'application/json',
24+
content: JSON.stringify(request.body),
25+
});
26+
}
27+
}
28+
29+
module.exports = ExampleAPIHandler;

index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* index.js - Loads the example API handler.
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
9+
'use strict';
10+
11+
const ExampleAPIHandler = require('./example-api-handler');
12+
13+
module.exports = (addonManager) => {
14+
new ExampleAPIHandler(addonManager);
15+
};

js/extension.js

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,47 @@
11
(function() {
2-
class ExampleExtension extends Extension {
2+
class ExampleExtension extends window.Extension {
33
constructor() {
44
super('example-extension');
5-
this.element = this.addMenuEntry('Example Extension');
6-
this.content = '';
5+
this.addMenuEntry('Example Extension');
76

7+
this.content = '';
88
fetch(`/extensions/${this.id}/views/content.html`)
99
.then((res) => res.text())
10-
.then((text) => this.content = text)
10+
.then((text) => {
11+
this.content = text;
12+
})
1113
.catch((e) => console.error('Failed to fetch content:', e));
1214
}
1315

1416
show() {
15-
this.element.innerHTML = this.content;
17+
this.view.innerHTML = this.content;
18+
19+
const key =
20+
document.getElementById('extension-example-extension-form-key');
21+
const value =
22+
document.getElementById('extension-example-extension-form-value');
23+
const submit =
24+
document.getElementById('extension-example-extension-form-submit');
25+
const pre =
26+
document.getElementById('extension-example-extension-response-data');
27+
28+
submit.addEventListener('click', () => {
29+
fetch(`/extensions/${this.id}/api/example-api?samplekey=samplevalue`, {
30+
method: 'POST',
31+
headers: {
32+
'Content-Type': 'application/json',
33+
Accept: 'application/json',
34+
Authorization: `Bearer ${window.API.jwt}`,
35+
},
36+
body: JSON.stringify({[key.value]: value.value}),
37+
}).then((res) => {
38+
return res.json();
39+
}).then((body) => {
40+
pre.innerText = JSON.stringify(body, null, 2);
41+
}).catch((e) => {
42+
pre.innerText = e.toString();
43+
});
44+
});
1645
}
1746
}
1847

0 commit comments

Comments
 (0)