Skip to content

Commit

Permalink
accept a base url
Browse files Browse the repository at this point in the history
for installations not on hosted on "fasterwebcloud.com"
  • Loading branch information
dangowans committed Nov 19, 2024
1 parent e85f38e commit e4a6378
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 28 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ npm install @cityssm/faster-url-builder
import { FasterUrlBuilder } from '@cityssm/faster-url-builder'

console.log(new FasterUrlBuilder('test-tenant').loginUrl)
// => https://test-tenant.fasterwebcloud.com/FASTER
// => https://test-tenant.fasterwebcloud.com/FASTER/Login
```

## Included URLs
Expand All @@ -40,8 +40,8 @@ console.log(new FasterUrlBuilder('test-tenant').loginUrl)

## Related Projects

[FASTER Web Report Exporter](https://github.com/cityssm/node-faster-report-exporter)<br />
[**FASTER Web Report Exporter**](https://github.com/cityssm/node-faster-report-exporter)<br />
On demand exports of selected reports from the FASTER Web Fleet Management System.

[FASTER Web Helper](https://github.com/cityssm/faster-web-helper)<br />
[**FASTER Web Helper**](https://github.com/cityssm/faster-web-helper)<br />
A service to support integrations with the FASTER Web fleet management system.
4 changes: 3 additions & 1 deletion eslint.config.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export { default } from 'eslint-config-cityssm';
import { type Config } from 'eslint-config-cityssm';
declare const config: Config;
export default config;
15 changes: 14 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
export { default } from 'eslint-config-cityssm';
import eslintConfigCityssm, { cspellWords, tseslint } from 'eslint-config-cityssm';
const config = tseslint.config(...eslintConfigCityssm, {
rules: {
'@cspell/spellchecker': [
'warn',
{
cspell: {
words: [...cspellWords, 'fasterwebcloud']
}
}
]
}
});
export default config;
21 changes: 20 additions & 1 deletion eslint.config.ts
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
export { default } from 'eslint-config-cityssm'
import eslintConfigCityssm, {
type Config,
cspellWords,
tseslint
} from 'eslint-config-cityssm'

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

export default config
13 changes: 10 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
type FasterBaseUrl = `https://${string}.fasterwebcloud.com/FASTER`;
export type FasterBaseUrl = `https://${string}.fasterwebcloud.com/FASTER`;
/**
* Tests if a base URL is valid.
* @param fasterBaseUrl - A possible FASTER Web base URL.
* @returns `true` if the base URL is valid.
*/
export declare function isValidBaseUrl(fasterBaseUrl: string): fasterBaseUrl is FasterBaseUrl;
export declare class FasterUrlBuilder {
#private;
/** Base URL */
Expand All @@ -9,9 +15,10 @@ export declare class FasterUrlBuilder {
readonly reportViewerUrl: `${FasterBaseUrl}/Domains/Reports/ReportViewer.aspx`;
/**
* Initializes the FasterUrlBuilder
* @param fasterTenant - The subdomain of the FASTER Web URL before ".fasterwebcloud.com"
* @param fasterTenantOrBaseUrl - The subdomain of the FASTER Web URL before ".fasterwebcloud.com"
* or the full domain and path including "/FASTER"
*/
constructor(fasterTenant: string);
constructor(fasterTenantOrBaseUrl: string);
/**
* Builds a URL for the inventory search.
* @param searchString - Item number search string
Expand Down
22 changes: 19 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
/**
* Tests if a base URL is valid.
* @param fasterBaseUrl - A possible FASTER Web base URL.
* @returns `true` if the base URL is valid.
*/
export function isValidBaseUrl(fasterBaseUrl) {
return (fasterBaseUrl.startsWith('https://') && fasterBaseUrl.endsWith('/FASTER'));
}
export class FasterUrlBuilder {
/** Base URL */
baseUrl;
/** Login URL */
loginUrl;
#inventorySearchUrl;
// eslint-disable-next-line no-secrets/no-secrets
#workOrderSearchUrl;
#workOrderUrl;
/** Report Viewer URL - Parameters required */
reportViewerUrl;
/**
* Initializes the FasterUrlBuilder
* @param fasterTenant - The subdomain of the FASTER Web URL before ".fasterwebcloud.com"
* @param fasterTenantOrBaseUrl - The subdomain of the FASTER Web URL before ".fasterwebcloud.com"
* or the full domain and path including "/FASTER"
*/
constructor(fasterTenant) {
this.baseUrl = `https://${fasterTenant}.fasterwebcloud.com/FASTER`;
constructor(fasterTenantOrBaseUrl) {
this.baseUrl = fasterTenantOrBaseUrl.startsWith('https')
? fasterTenantOrBaseUrl
: `https://${fasterTenantOrBaseUrl}.fasterwebcloud.com/FASTER`;
if (!isValidBaseUrl(this.baseUrl)) {
throw new Error(`Invalid base URL: ${this.baseUrl}`);
}
this.loginUrl = `${this.baseUrl}/Login`;
/* Inventory */
this.#inventorySearchUrl = `${this.baseUrl}/Domains/Parts/Search/Default.aspx?xact=False&type=False&str=`;
/* Maintenance */
// eslint-disable-next-line no-secrets/no-secrets
this.#workOrderSearchUrl = `${this.baseUrl}/Domains/Maintenance/WorkOrder/Search/Default.aspx?xact=False&type=False&str=`;
this.#workOrderUrl = `${this.baseUrl}/Domains/Maintenance/WorkOrder/WorkOrderMaster.aspx?workOrderID=`;
/* Reports */
Expand Down
31 changes: 27 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
type FasterBaseUrl = `https://${string}.fasterwebcloud.com/FASTER`
export type FasterBaseUrl = `https://${string}.fasterwebcloud.com/FASTER`

/**
* Tests if a base URL is valid.
* @param fasterBaseUrl - A possible FASTER Web base URL.
* @returns `true` if the base URL is valid.
*/
export function isValidBaseUrl(
fasterBaseUrl: string
): fasterBaseUrl is FasterBaseUrl {
return (
fasterBaseUrl.startsWith('https://') && fasterBaseUrl.endsWith('/FASTER')
)
}

export class FasterUrlBuilder {
/** Base URL */
Expand All @@ -9,6 +22,7 @@ export class FasterUrlBuilder {

readonly #inventorySearchUrl: `${FasterBaseUrl}/Domains/Parts/Search/Default.aspx?xact=False&type=False&str=`

// eslint-disable-next-line no-secrets/no-secrets
readonly #workOrderSearchUrl: `${FasterBaseUrl}/Domains/Maintenance/WorkOrder/Search/Default.aspx?xact=False&type=False&str=`
readonly #workOrderUrl: `${FasterBaseUrl}/Domains/Maintenance/WorkOrder/WorkOrderMaster.aspx?workOrderID=`

Expand All @@ -17,16 +31,25 @@ export class FasterUrlBuilder {

/**
* Initializes the FasterUrlBuilder
* @param fasterTenant - The subdomain of the FASTER Web URL before ".fasterwebcloud.com"
* @param fasterTenantOrBaseUrl - The subdomain of the FASTER Web URL before ".fasterwebcloud.com"
* or the full domain and path including "/FASTER"
*/
constructor(fasterTenant: string) {
this.baseUrl = `https://${fasterTenant}.fasterwebcloud.com/FASTER`
constructor(fasterTenantOrBaseUrl: string) {
this.baseUrl = fasterTenantOrBaseUrl.startsWith('https')
? (fasterTenantOrBaseUrl as FasterBaseUrl)
: `https://${fasterTenantOrBaseUrl}.fasterwebcloud.com/FASTER`

if (!isValidBaseUrl(this.baseUrl)) {
throw new Error(`Invalid base URL: ${this.baseUrl as string}`)
}

this.loginUrl = `${this.baseUrl}/Login`

/* Inventory */
this.#inventorySearchUrl = `${this.baseUrl}/Domains/Parts/Search/Default.aspx?xact=False&type=False&str=`

/* Maintenance */
// eslint-disable-next-line no-secrets/no-secrets
this.#workOrderSearchUrl = `${this.baseUrl}/Domains/Maintenance/WorkOrder/Search/Default.aspx?xact=False&type=False&str=`
this.#workOrderUrl = `${this.baseUrl}/Domains/Maintenance/WorkOrder/WorkOrderMaster.aspx?workOrderID=`

Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@cityssm/faster-url-builder",
"type": "module",
"version": "0.1.0",
"version": "0.2.0",
"description": "Builds URLs for the FASTER Web Fleet Management System",
"exports": "./index.js",
"scripts": {
Expand All @@ -22,7 +22,7 @@
},
"homepage": "https://github.com/cityssm/node-faster-url-builder#readme",
"devDependencies": {
"@types/node": "^22.9.0",
"@types/node": "^22.9.1",
"eslint-config-cityssm": "^15.2.0",
"prettier-config-cityssm": "^1.0.0"
}
Expand Down
28 changes: 26 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import assert from 'node:assert';
import { describe, it } from 'node:test';
import { FasterUrlBuilder } from '../index.js';
import { FasterUrlBuilder, isValidBaseUrl } from '../index.js';
await describe('faster-url-builder', async () => {
const tenant = 'faster-tenant';
const fasterUrlBuilder = new FasterUrlBuilder(tenant);
await it('Constructs a proper base URL', () => {
await it('Initializes with a full base URL', () => {
const testBaseUrl = 'https://test.example.com/FASTER';
const fasterUrlBuilderFromUrl = new FasterUrlBuilder(testBaseUrl);
assert.strictEqual(fasterUrlBuilderFromUrl.baseUrl, testBaseUrl);
});
await it('Constructs a proper base URL from a tenant', () => {
assert(fasterUrlBuilder.baseUrl.includes(tenant));
});
await it('Constructs a proper login URL', () => {
Expand Down Expand Up @@ -32,3 +37,22 @@ await describe('faster-url-builder', async () => {
assert(workOrderUrl.endsWith(workOrderNumber.toString()));
});
});
await describe('faster-url-builder/errors', async () => {
await it('Rejects invalid base URLs', () => {
// http link
assert.strictEqual(isValidBaseUrl('http://test.example.com/FASTER'), false);
// missing "/FASTER"
assert.strictEqual(isValidBaseUrl('https://test.example.com'), false);
let declaredSuccessfully = false;
// eslint-disable-next-line @typescript-eslint/init-declarations
let builder;
try {
builder = new FasterUrlBuilder('https://test.example.com/FASTE');
declaredSuccessfully = true;
}
catch { }
if (declaredSuccessfully) {
assert.fail(`URL builder declared successfully with invalid URL: ${builder.baseUrl}`);
}
});
});
35 changes: 33 additions & 2 deletions test/test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import assert from 'node:assert'
import { describe, it } from 'node:test'

import { FasterUrlBuilder } from '../index.js'
import { FasterUrlBuilder, isValidBaseUrl } from '../index.js'

await describe('faster-url-builder', async () => {
const tenant = 'faster-tenant'
const fasterUrlBuilder = new FasterUrlBuilder(tenant)

await it('Constructs a proper base URL', () => {
await it('Initializes with a full base URL', () => {
const testBaseUrl = 'https://test.example.com/FASTER'
const fasterUrlBuilderFromUrl = new FasterUrlBuilder(testBaseUrl)
assert.strictEqual(fasterUrlBuilderFromUrl.baseUrl, testBaseUrl)
})

await it('Constructs a proper base URL from a tenant', () => {
assert(fasterUrlBuilder.baseUrl.includes(tenant))
})

Expand Down Expand Up @@ -51,3 +57,28 @@ await describe('faster-url-builder', async () => {
assert(workOrderUrl.endsWith(workOrderNumber.toString()))
})
})

await describe('faster-url-builder/errors', async () => {
await it('Rejects invalid base URLs', () => {
// http link
assert.strictEqual(isValidBaseUrl('http://test.example.com/FASTER'), false)

// missing "/FASTER"
assert.strictEqual(isValidBaseUrl('https://test.example.com'), false)

let declaredSuccessfully = false

// eslint-disable-next-line @typescript-eslint/init-declarations
let builder: FasterUrlBuilder | undefined

try {
builder = new FasterUrlBuilder('https://test.example.com/FASTE')
declaredSuccessfully = true
} catch {}
if (declaredSuccessfully) {
assert.fail(
`URL builder declared successfully with invalid URL: ${(builder as FasterUrlBuilder).baseUrl}`
)
}
})
})

0 comments on commit e4a6378

Please sign in to comment.