Skip to content

Commit

Permalink
Merge branch 'william/update-linting'
Browse files Browse the repository at this point in the history
  • Loading branch information
swansontec committed Jul 18, 2022
2 parents de7b060 + 46101f7 commit f746d36
Show file tree
Hide file tree
Showing 14 changed files with 290 additions and 318 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/build/
/lib/
18 changes: 13 additions & 5 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
{
"extends": [
"standard-kit/prettier",
"standard-kit/prettier/node",
"standard-kit/prettier/typescript"
"standard-kit/prettier/typescript",
"standard-kit/prettier/node"
],
"overrides": [
{
"files": ["*.ts", "*.tsx"],
"rules": {
"@typescript-eslint/explicit-function-return-type": "warn",
"@typescript-eslint/no-dynamic-delete": "warn",
"@typescript-eslint/strict-boolean-expressions": "warn"
}
}
],
"parserOptions": {
"project": "tsconfig.json"
Expand All @@ -11,8 +21,6 @@
"simple-import-sort"
],
"rules": {
"@typescript-eslint/explicit-function-return-type": "warn",
"@typescript-eslint/strict-boolean-expressions": "warn",
"simple-import-sort/sort": "error"
"simple-import-sort/imports": "error"
}
}
21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
"scripts": {
"build": "sucrase -q -t typescript,imports -d ./lib ./src",
"clean": "rimraf lib",
"fix": "eslint . --fix",
"fix": "yarn-deduplicate && eslint . --fix",
"lint": "eslint .",
"precommit": "lint-staged && npm-run-all types prepare",
"prepare": "husky install && npm-run-all clean build",
"start-server": "node -r sucrase/register src/server/index.ts",
"start-price": "node -r sucrase/register src/price-script/index.ts",
"types": "tsc"
"types": "tsc",
"verify": "npm-run-all lint types prepare"
},
"lint-staged": {
"*.{js,ts}": "eslint"
Expand All @@ -38,21 +39,21 @@
"@types/express": "^4.17.8",
"@types/node": "^14.0.5",
"@types/node-schedule": "^1.3.0",
"@typescript-eslint/eslint-plugin": "^2.0.0",
"@typescript-eslint/parser": "^2.0.0",
"eslint": "^7.9.0",
"eslint-config-standard-kit": "^0.14.4",
"eslint-plugin-import": "^2.22.0",
"@typescript-eslint/eslint-plugin": "^4.8.2",
"@typescript-eslint/parser": "^4.8.2",
"eslint": "^7.14.0",
"eslint-config-standard-kit": "0.15.1",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-simple-import-sort": "^5.0.3",
"eslint-plugin-standard": "^4.0.1",
"eslint-plugin-simple-import-sort": "^6.0.1",
"husky": "^7.0.0",
"lint-staged": "^10.4.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.1.2",
"sucrase": "^3.21.0",
"typescript": "^4.7.3"
"typescript": "^4.7.3",
"yarn-deduplicate": "^5.0.0"
}
}
2 changes: 1 addition & 1 deletion src/models/CurrencyThreshold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class CurrencyThreshold extends Base implements ICurrencyThreshold {
currencyCode: string
): Promise<CurrencyThreshold> {
const threshold = new CurrencyThreshold(undefined, currencyCode)
return threshold.save()
return await threshold.save()
}

public async update(
Expand Down
2 changes: 1 addition & 1 deletion src/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class User extends Base implements ReturnType<typeof asUser> {
currencyCode: string,
hours: string
) {
return User.table.view<IDevicesByCurrencyHoursViewResponse>(
return await User.table.view<IDevicesByCurrencyHoursViewResponse>(
'filter',
'by-currency',
{ key: [currencyCode, hours] }
Expand Down
12 changes: 6 additions & 6 deletions src/models/User/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ export const views = {
filter: {
// @ts-expect-error
byCurrency(doc) {
var notifs = doc.notifications
if (notifs && notifs.enabled && notifs.currencyCodes) {
var codes = notifs.currencyCodes
for (var currencyCode in codes) {
for (var hours in codes[currencyCode]) {
var enabled = codes[currencyCode][hours]
const notifs = doc.notifications
if (notifs?.enabled && notifs.currencyCodes) {
const codes = notifs.currencyCodes
for (const currencyCode in codes) {
for (const hours in codes[currencyCode]) {
const enabled = codes[currencyCode][hours]
if (enabled) {
emit([currencyCode, hours], doc.devices)
}
Expand Down
6 changes: 2 additions & 4 deletions src/models/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class Base implements ReturnType<typeof asModelData> {
}

public processAPIResponse(response: Nano.DocumentInsertResponse) {
if (response.ok === true) {
if (response.ok) {
this._id = response.id
this._rev = response.rev
}
Expand Down Expand Up @@ -85,7 +85,6 @@ export class Base implements ReturnType<typeof asModelData> {
): Promise<Array<InstanceType<T>>> {
const response = await this.table.list({ include_docs: true })
return response.rows.map(row => {
// @ts-ignore
const item: InstanceType<T> = new this(row.doc)
item.validate()
return item
Expand All @@ -98,7 +97,6 @@ export class Base implements ReturnType<typeof asModelData> {
): Promise<Array<InstanceType<T>>> {
const response = await this.table.find(where)
return response.docs.map(doc => {
// @ts-ignore
const item: InstanceType<T> = new this(doc)
item.validate()
return item
Expand Down Expand Up @@ -151,7 +149,7 @@ export class Base implements ReturnType<typeof asModelData> {
'Document already exists. Fetching current `_rev` and resaving.'
)
const { _rev } = await ItemClass.fetch(this._id)
return this.save('_rev', _rev)
return await this.save('_rev', _rev)
}
default:
throw err
Expand Down
4 changes: 2 additions & 2 deletions src/price-script/checkPriceChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export async function checkPriceChanges(manager: NotificationManager) {
const body = `${currencyCode} is ${direction} ${symbol}${priceChange}% to $${displayPrice} in the last ${time}.`
const data = {}

return manager.send(title, body, deviceTokens, data)
return await manager.send(title, body, deviceTokens, data)
}

// Fetch list of threshold items and their prices
Expand All @@ -60,7 +60,7 @@ export async function checkPriceChanges(manager: NotificationManager) {
defaultThresholds[hours],
anomalyPercent
)
if (!priceData) continue
if (priceData == null) continue

const { rows: usersDevices } = await User.devicesByCurrencyHours(
currencyCode,
Expand Down
4 changes: 2 additions & 2 deletions src/price-script/fetchThresholdPrices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Counter = ReturnType<typeof io.counter>
const processMetrics: { [id: string]: Counter | undefined } = {}

async function sleep(ms = SLEEP_TIMEOUT) {
return new Promise(resolve => setTimeout(resolve, ms))
return await new Promise(resolve => setTimeout(resolve, ms))
}
export async function fetchThresholdPrice(
currencyThreshold: CurrencyThreshold,
Expand Down Expand Up @@ -87,7 +87,7 @@ export async function fetchThresholdPrice(

const counterId = `threshold:crossed:${currencyCode}:${hours}`
let counter = processMetrics[counterId]
if (!counter) {
if (counter == null) {
counter = processMetrics[counterId] = io.counter({
id: counterId,
name: `Threshold Crossed for ${currencyCode} - ${hours} Hour`
Expand Down
4 changes: 2 additions & 2 deletions src/price-script/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ async function main(): Promise<void> {

// Read the API keys from settings:
const managers = await Promise.all(
syncedSettings.doc.apiKeys.map(async partner =>
NotificationManager.init(partner.apiKey)
syncedSettings.doc.apiKeys.map(
async partner => await NotificationManager.init(partner.apiKey)
)
)

Expand Down
2 changes: 1 addition & 1 deletion src/price-script/prices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export async function getPrice(
} = await rates.get(`?currency_pair=${base}_${quote}${dateString}`)
const rate = asNumber(parseFloat(exchangeRate))
if (!/^\d+(\.\d+)?/.test(exchangeRate.toString())) {
throw new Error(`${base}/${quote} rate given was ${exchangeRate}`)
throw new Error(`${base}/${quote} rate given was ${String(exchangeRate)}`)
}
return rate
} catch (err: any) {
Expand Down
2 changes: 1 addition & 1 deletion src/server/controllers/NotificationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ NotificationController.post('/send', async (req, res) => {
res.json(response)
} catch (err) {
console.error(
`Failed to send notifications to user ${req.body.userId} devices`,
`Failed to send notifications to user ${String(req.body.userId)} devices`,
err
)
res.status(500).json(err)
Expand Down
28 changes: 21 additions & 7 deletions src/server/controllers/UserController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ UserController.get('/', async (req, res) => {

res.json(result)
} catch (err) {
console.error(`Failed to get user settings for ${req.query.userId}`, err)
console.error(
`Failed to get user settings for ${String(req.query.userId)}`,
err
)
res.status(500).json(err)
}
})
Expand All @@ -43,7 +46,10 @@ UserController.post('/device/attach', async (req, res) => {

res.json(user)
} catch (err) {
console.error(`Failed to attach device to user ${req.query.userId}`, err)
console.error(
`Failed to attach device to user ${String(req.query.userId)}`,
err
)
res.status(500).json(err)
}
})
Expand All @@ -63,12 +69,16 @@ UserController.post('/notifications', async (req, res) => {
const user = await User.fetch(userId)
await user.registerNotifications(currencyCodes)

console.log(`Registered notifications for user ${userId}: ${currencyCodes}`)
console.log(
`Registered notifications for user ${userId}: ${String(currencyCodes)}`
)

res.json(user)
} catch (err) {
console.error(
`Failed to register for notifications for user ${req.query.userId}`,
`Failed to register for notifications for user ${String(
req.query.userId
)}`,
err
)
res.status(500).json(err)
Expand Down Expand Up @@ -97,7 +107,9 @@ UserController.get('/notifications/:currencyCode', async (req, res) => {
res.json(notificationSettings)
} catch (err) {
console.error(
`Failed to get notification settings for user ${req.query.userId} for ${req.params.currencyCode}`,
`Failed to get notification settings for user ${String(
req.query.userId
)} for ${req.params.currencyCode}`,
err
)
res.status(500).json(err)
Expand Down Expand Up @@ -134,7 +146,9 @@ UserController.put('/notifications/:currencyCode', async (req, res) => {
res.json(currencySettings)
} catch (err) {
console.error(
`Failed to update notification settings for user ${req.query.userId} for ${req.params.currencyCode}`,
`Failed to update notification settings for user ${String(
req.query.userId
)} for ${req.params.currencyCode}`,
err
)
res.status(500).json(err)
Expand All @@ -159,7 +173,7 @@ UserController.post('/notifications/toggle', async (req, res) => {
user.notifications.enabled = enabled
await user.save()

console.log(`User notifications toggled to: ${enabled}`)
console.log(`User notifications toggled to: ${String(enabled)}`)

res.json(user)
} catch (err) {
Expand Down
Loading

0 comments on commit f746d36

Please sign in to comment.