Skip to content

Commit

Permalink
dependency updates and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
dangowans committed Nov 20, 2024
1 parent 3aa7411 commit 6d3d3b6
Show file tree
Hide file tree
Showing 26 changed files with 8,033 additions and 2,034 deletions.
2 changes: 0 additions & 2 deletions .eslintignore

This file was deleted.

10 changes: 0 additions & 10 deletions .eslintrc.json

This file was deleted.

4 changes: 2 additions & 2 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ jobs:
- name: Install Application
run: |
npm ci
npm install -g mocha c8
npm install -g c8
- name: Run Coverage Testing
run: c8 --reporter=lcov --reporter=text --reporter=text-summary mocha --timeout 10000 --exit
run: npm run coverage

- name: Code Climate
if: ${{ github.event_name != 'pull_request' && env.CC_TEST_REPORTER_ID != '' }}
Expand Down
10 changes: 4 additions & 6 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ test

.codeclimate.yml
.deepsource.toml
.eslintignore
.eslintrc.json
.gitignore
.npmignore
.prettierignore
.prettierrc.json
cli.d.ts
cli.js
cli.ts

cli.*
eslint.config.*
prettier.config.*
tsconfig.json
7 changes: 0 additions & 7 deletions .prettierrc.json

This file was deleted.

5 changes: 5 additions & 0 deletions browserGlobal.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import puppeteerLaunch from '@cityssm/puppeteer-launch';
import exitHook from 'exit-hook';
import { clearIntervalAsync, setIntervalAsync } from 'set-interval-async/dynamic';
/*
* Browser Global
*/
export const pageTimeoutMillis = 90_000;
const browserStartupTimeoutMillis = 3 * 60_000;
const browserGlobalExpiryMillis = Math.max(browserStartupTimeoutMillis, pageTimeoutMillis) + 10_000;
Expand Down Expand Up @@ -33,13 +36,15 @@ export async function cleanUpBrowserGlobal(useForce = false) {
await browserGlobal?.close();
}
catch {
// ignore
}
browserGlobal = undefined;
if (browserGlobalTimer) {
try {
await clearIntervalAsync(browserGlobalTimer);
}
catch {
// ignore
}
browserGlobalTimer = undefined;
}
Expand Down
5 changes: 5 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
// eslint-disable-next-line eslint-comments/disable-enable-pair
/* eslint-disable @typescript-eslint/naming-convention */
// Search Form
export const clearanceStart_url = 'https://clearances.wsib.ca/Clearances/eclearance/start';
export const clearanceStart_searchFormSelector = '#TOKENSimpleSearchForm';
export const clearanceStart_searchFieldSelector = '#simpleAccountNumbersTOKEN';
// Search Results
export const clearanceResult_certificateLinkSelector = "#eClearanceWorkspaceTargSubDivFormXX .fancytable a[rel='eClearanceWorkspaceContent'][href^='GCSearchCertDet']";
export const clearanceResult_certificateBadStandingSelector = '#eClearanceWorkspaceTargSubDivFormXX .fancytable .badstanding';
export const clearanceResult_defaultErrorMessage = 'Clearance certificate link not found.';
// Certificate
export const certificate_tableSelector = '#eClearanceWorkspaceDivForm .fancytable';
export const certificateField_contractorLegalTradeName = 'Contractor Legal / Trade Name';
export const certificateField_contractorAddress = 'Contractor Address';
Expand Down
3 changes: 3 additions & 0 deletions eslint.config.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { type Config } from 'eslint-config-cityssm';
declare const config: Config;
export default config;
14 changes: 14 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import eslintConfigCityssm, { cspellWords, tseslint } from 'eslint-config-cityssm';
const config = tseslint.config(...eslintConfigCityssm, {
rules: {
'@cspell/spellchecker': [
'warn',
{
cspell: {
words: [...cspellWords, 'naics', 'wsib']
}
}
]
}
});
export default config;
20 changes: 20 additions & 0 deletions eslint.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import eslintConfigCityssm, {
type Config,
cspellWords,
tseslint
} from 'eslint-config-cityssm'

const config = tseslint.config(...eslintConfigCityssm, {
rules: {
'@cspell/spellchecker': [
'warn',
{
cspell: {
words: [...cspellWords, 'naics', 'wsib']
}
}
]
}
}) as Config

export default config
8 changes: 8 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import type { WSIBClearance_Failure, WSIBClearance_Success } from './types.js';
/**
* Retrieves a WSIB clearance certificate from the WSIB website.
* @param accountNumber - The WSIB account number
* @returns The WSIB clearance certificate data.
*/
export declare function getClearanceByAccountNumber(accountNumber: string): Promise<WSIBClearance_Failure | WSIBClearance_Success>;
/**
* Closes the cached web browser.
*/
export declare function cleanUpBrowser(): Promise<void>;
export default getClearanceByAccountNumber;
31 changes: 22 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,23 @@ function cleanRawCertificateOutput(rawOutput) {
principalAddress
};
}
/**
* Retrieves a WSIB clearance certificate from the WSIB website.
* @param accountNumber - The WSIB account number
* @returns The WSIB clearance certificate data.
*/
export async function getClearanceByAccountNumber(accountNumber) {
let page;
try {
const browser = await browserGlobal.getBrowserGlobal();
page = await browser.newPage();
// Set up page options
page.setDefaultNavigationTimeout(browserGlobal.pageTimeoutMillis);
page.setDefaultTimeout(browserGlobal.pageTimeoutMillis);
await page.setExtraHTTPHeaders({
'Accept-Language': 'en'
});
// Load eservice
const pageResponse = await page.goto(config.clearanceStart_url, {
referer: 'https://www.wsib.ca/en',
waitUntil: 'domcontentloaded'
Expand All @@ -41,6 +48,7 @@ export async function getClearanceByAccountNumber(accountNumber) {
}
browserGlobal.keepBrowserGlobalAlive();
await page.waitForSelector('body');
// Fill out form
await page.$eval(config.clearanceStart_searchFieldSelector, (inputElement, accountNumberValue) => {
inputElement.value = accountNumberValue;
}, accountNumber);
Expand All @@ -49,6 +57,7 @@ export async function getClearanceByAccountNumber(accountNumber) {
});
browserGlobal.keepBrowserGlobalAlive();
await page.waitForSelector('body');
// Find result link
let hasError = false;
await page
.$eval(config.clearanceResult_certificateLinkSelector, (linkElement) => {
Expand All @@ -59,18 +68,17 @@ export async function getClearanceByAccountNumber(accountNumber) {
});
if (hasError) {
const errorMessage = await page
.$eval(config.clearanceResult_certificateBadStandingSelector, (badStandingElement) => {
return badStandingElement
? badStandingElement.textContent
: config.clearanceResult_defaultErrorMessage;
})
.$eval(config.clearanceResult_certificateBadStandingSelector, (badStandingElement) => badStandingElement
? badStandingElement.textContent
: config.clearanceResult_defaultErrorMessage)
.catch(() => {
throw new Error(config.clearanceResult_defaultErrorMessage);
});
throw new Error(errorMessage ?? '');
}
browserGlobal.keepBrowserGlobalAlive();
await page.waitForSelector('body');
// Parse the certificate
const certificateURL = page.url();
const parsedTable = await page.$eval(config.certificate_tableSelector, (tableElement) => {
const parsedTableValue = {};
Expand All @@ -83,19 +91,20 @@ export async function getClearanceByAccountNumber(accountNumber) {
return parsedTableValue;
});
const certificate = cleanRawCertificateOutput(parsedTable);
return Object.assign({
return {
success: true,
accountNumber
}, certificate, {
accountNumber,
...certificate,
certificateURL
});
};
}
catch (error) {
let errorURL = '';
try {
errorURL = page?.url() ?? '';
}
catch {
// ignore
}
return {
success: false,
Expand All @@ -111,9 +120,13 @@ export async function getClearanceByAccountNumber(accountNumber) {
}
}
catch {
// ignore
}
}
}
/**
* Closes the cached web browser.
*/
export async function cleanUpBrowser() {
await browserGlobal.cleanUpBrowserGlobal(true);
}
Expand Down
24 changes: 9 additions & 15 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ function cleanRawCertificateOutput(

/**
* Retrieves a WSIB clearance certificate from the WSIB website.
* @param {string} accountNumber - The WSIB account number
* @returns {Promise<WSIBClearance_Failure | WSIBClearance_Success>} - The WSIB clearance certificate data.
* @param accountNumber - The WSIB account number
* @returns The WSIB clearance certificate data.
*/
export async function getClearanceByAccountNumber(
accountNumber: string
Expand Down Expand Up @@ -125,11 +125,9 @@ export async function getClearanceByAccountNumber(
const errorMessage = await page
.$eval(
config.clearanceResult_certificateBadStandingSelector,
(badStandingElement: HTMLElement) => {
return badStandingElement
(badStandingElement: HTMLElement) => badStandingElement
? badStandingElement.textContent
: config.clearanceResult_defaultErrorMessage
}
)
.catch(() => {
throw new Error(config.clearanceResult_defaultErrorMessage)
Expand Down Expand Up @@ -165,16 +163,12 @@ export async function getClearanceByAccountNumber(

const certificate = cleanRawCertificateOutput(parsedTable)

return Object.assign(
{
success: true,
accountNumber
},
certificate,
{
certificateURL
}
)
return {
success: true,
accountNumber,
...certificate,
certificateURL
}
} catch (error) {
let errorURL = ''

Expand Down
Loading

0 comments on commit 6d3d3b6

Please sign in to comment.