Skip to content

Commit 3bc997d

Browse files
committed
flow fixes
1 parent 4cd5d4e commit 3bc997d

File tree

8 files changed

+45
-44
lines changed

8 files changed

+45
-44
lines changed

src/lib/controllers/Controller.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
// @flow
22

3-
// todo annotate better
3+
import type { HttpResult } from './types';
4+
45
export default class Controller {
5-
bad = (message: string): Object => ({
6+
bad = (message: string): HttpResult<*> => ({
67
data: { message },
78
status: 400,
89
});
910

10-
ok = (output?: Object): Object => ({
11+
ok = <TType>(output?: TType): HttpResult<TType> => ({
1112
data: output,
1213
status: 200,
1314
});

src/lib/controllers/UsersController.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class UsersController extends Controller {
2222
@httpVerb('post')
2323
@route('/v1/users')
2424
@anonymous()
25-
async createUser(userCredentials: UserCredentials): Promise<Object> {
25+
async createUser(userCredentials: UserCredentials): Promise<*> {
2626
try {
2727
const isUserNameInUse =
2828
this._usersRepository.isUserNameInUse(userCredentials.username);
@@ -41,7 +41,7 @@ class UsersController extends Controller {
4141
@httpVerb('delete')
4242
@route('/v1/access_tokens/:token')
4343
@anonymous()
44-
async deleteAccessToken(token: string): Promise<Object> {
44+
async deleteAccessToken(token: string): Promise<*> {
4545
try {
4646
const { username, password } = basicAuthParser(this.request.get('authorization'));
4747
const user = await this._usersRepository.validateLogin(username, password);
@@ -57,7 +57,7 @@ class UsersController extends Controller {
5757
@httpVerb('get')
5858
@route('/v1/access_tokens')
5959
@anonymous()
60-
async getAccessTokens(): Promise<Object> {
60+
async getAccessTokens(): Promise<*> {
6161
try {
6262
const { username, password } = basicAuthParser(this.request.get('authorization'));
6363
const user = await this._usersRepository.validateLogin(username, password);

src/lib/controllers/WebhookController.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,49 +15,49 @@ const REQUEST_TYPES: Array<RequestType> = [
1515
'DELETE', 'GET', 'POST', 'PUT',
1616
];
1717

18-
const validateWebhookModel = (webhook: Webhook): ?Error => {
19-
if (!webhook.event) {
18+
const validateWebhookMutator = (webhookMutator: WebhookMutator): ?Error => {
19+
if (!webhookMutator.event) {
2020
return new Error('no event name provided');
2121
}
22-
if (!webhook.url) {
22+
if (!webhookMutator.url) {
2323
return new Error('no url provided');
2424
}
25-
if (!webhook.requestType) {
25+
if (!webhookMutator.requestType) {
2626
return new Error('no requestType provided');
2727
}
28-
if (!REQUEST_TYPES.includes(webhook.requestType)) {
28+
if (!REQUEST_TYPES.includes(webhookMutator.requestType)) {
2929
return new Error('wrong requestType');
3030
}
3131

3232
return null;
3333
};
3434

3535
class WebhookController extends Controller {
36-
_webhookRepository: Repository<Webhook>;
36+
_webhookRepository: Repository<Webhook, WebhookMutator>;
3737

38-
constructor(webhookRepository: Repository<Webhook>) {
38+
constructor(webhookRepository: Repository<Webhook, WebhookMutator>) {
3939
super();
4040

4141
this._webhookRepository = webhookRepository;
4242
}
4343

4444
@httpVerb('get')
4545
@route('/v1/webhooks')
46-
getAll(): Object {
46+
async getAll(): Promise<*> {
4747
return this.ok(this._webhookRepository.getAll());
4848
}
4949

5050
@httpVerb('get')
5151
@route('/v1/webhooks/:webhookId')
52-
getById(webhookId: string): Object {
52+
async getById(webhookId: string): Promise<*> {
5353
return this.ok(this._webhookRepository.getById(webhookId));
5454
}
5555

5656
@httpVerb('post')
5757
@route('/v1/webhooks')
58-
create(model: WebhookMutator): Object {
58+
async create(model: WebhookMutator): Promise<*> {
5959
try {
60-
const validateError = validateWebhookModel(model);
60+
const validateError = validateWebhookMutator(model);
6161
if (validateError) {
6262
throw validateError;
6363
}
@@ -77,7 +77,7 @@ class WebhookController extends Controller {
7777

7878
@httpVerb('delete')
7979
@route('/v1/webhooks/:webhookId')
80-
deleteById(webhookId: string): Object {
80+
async deleteById(webhookId: string): Promise<*> {
8181
this._webhookRepository.deleteById(webhookId);
8282
return this.ok();
8383
}

src/lib/controllers/types.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @flow
2+
3+
4+
export type HttpResult<TType> = {
5+
data: ?TType,
6+
status: number,
7+
} | {
8+
data: ?string,
9+
status: number,
10+
};

src/lib/repository/UsersFileRepository.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class UsersFileRepository {
4141
getByUsername = (username: string): ?User =>
4242
this.getAll().find((user: User): boolean => user.username === username);
4343

44-
async validateLogin(username: string, password: string): User {
44+
async validateLogin(username: string, password: string): Promise<User> {
4545
try {
4646
const user = this.getByUsername(username);
4747
if (!user) {
@@ -60,7 +60,7 @@ class UsersFileRepository {
6060
}
6161

6262
getByAccessToken = (accessToken: string): ?User =>
63-
this.getAll().find((user: User): User =>
63+
this.getAll().find((user: User): boolean =>
6464
user.accessTokens.some((tokenObject: TokenObject): boolean =>
6565
tokenObject.accessToken === accessToken,
6666
),

src/lib/repository/WebhookFileRepository.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @flow
22

3-
import type { Webhook } from '../../types';
3+
import type { Webhook, WebhookMutator } from '../../types';
44

55
import { FileManager, uuid } from 'spark-protocol';
66

@@ -11,7 +11,7 @@ class WebhookFileRepository {
1111
this._fileManager = new FileManager(path);
1212
}
1313

14-
create = (model: Webhook): Webhook => {
14+
create = (model: WebhookMutator): Webhook => {
1515
const modelToSave = {
1616
...model,
1717
created_at: new Date(),
@@ -33,7 +33,6 @@ class WebhookFileRepository {
3333

3434
getById = (id: string): Webhook =>
3535
this._fileManager.getFile(`${id}.json`);
36-
3736
}
3837

3938
export default WebhookFileRepository;

src/types.js

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
11
// @flow
22

3-
export type Webhook = {
4-
deviceID: string,
5-
event: string,
6-
errorResponseTopic: string,
3+
export type Webhook = WebhookMutator & {
4+
created_at: Date,
75
id: string,
8-
json: {[key: string]: Object},
9-
mydevices: boolean,
10-
productIdOrSlug: ?string,
11-
rejectUnauthorized: boolean,
12-
requestType: RequestType,
13-
responseTemplate: ?string,
14-
responseTopic: string,
15-
url: string,
166
};
177

188
export type WebhookMutator = {
199
auth?: { Authorization: string },
20-
deviceID?: boolean,
10+
deviceID?: string,
2111
errorResponseTopic?: string,
2212
event: string,
2313
form?: { [key: string]: Object },
24-
headers?:{ [key: string]: string },
14+
headers?: { [key: string]: string },
2515
json?: { [key: string]: Object },
2616
mydevices?: boolean,
2717
noDefaults?: boolean,
@@ -69,6 +59,7 @@ export type User = {
6959
created_at: Date,
7060
id: string,
7161
passwordHash: string,
62+
salt: string,
7263
username: string,
7364
};
7465

@@ -77,16 +68,16 @@ export type UserCredentials = {
7768
password: string,
7869
};
7970

80-
export type Repository<TModel> = {
81-
create: (id: string, model: TModel) => TModel,
71+
export type Repository<TModel, TMutator> = {
72+
create: (model: TMutator) => TModel,
8273
deleteById: (id: string) => void,
8374
getAll: () => Array<TModel>,
8475
getById: (id: string) => TModel,
8576
update: (id: string, model: TModel) => TModel,
8677
};
8778

88-
export type UsersRepository = Repository<User> & {
89-
deleteAccessToken: (accessToken: string) => void,
79+
export type UsersRepository = Repository<User, UserCredentials> & {
80+
deleteAccessToken: (user: User, accessToken: string) => void,
9081
getByAccessToken: (accessToken: string) => User,
9182
getByUsername: (username: string) => ?User,
9283
isUserNameInUse: (username: string) => boolean,
@@ -112,6 +103,6 @@ export type Settings = {
112103
serverKeyFile: string,
113104
serverKeyPassEnvVar: ?string,
114105
serverKeyPassFile: ?string,
115-
usersRepository: Repository<*>,
116-
webhookRepository: Repository<*>,
106+
usersRepository: Repository<*, *>,
107+
webhookRepository: Repository<*, *>,
117108
};

test/WebhooksController.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ test.serial('should return webhook object by id', async t => {
135135
t.is(testWebhook.url, response.body.url);
136136
});
137137

138-
test.serial('should deleteById webhook', async t => {
138+
test.serial('should delete webhook', async t => {
139139
const deleteResponse = await request(app)
140140
.delete(`/v1/webhooks/${testWebhook.id}`)
141141
.query({ access_token: userToken });

0 commit comments

Comments
 (0)