-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: better tsdoc comments and documentation
- Loading branch information
Showing
13 changed files
with
192 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
|
||
export const scanDir = (dir: string) => { | ||
let res = fs | ||
.readdirSync(path.resolve(__dirname, `../${dir}`)) | ||
.filter((item) => !item.startsWith(".")); | ||
if (res) { | ||
const arr = []; | ||
for (let item of res) { | ||
const parts = item.split("."); | ||
|
||
let currItem = arr; | ||
let currentPath = ""; | ||
while (parts.length > 2) { | ||
const part = parts.shift(); | ||
currentPath += (currentPath === "" ? "" : ".") + part; | ||
const found = currItem.find((item) => item.text === part); | ||
if (found) { | ||
currItem = found.items; | ||
} else { | ||
const newItem = { | ||
text: part, | ||
items: [], | ||
link: path.join(dir, currentPath), | ||
}; | ||
currItem.push(newItem); | ||
currItem = newItem.items; | ||
} | ||
} | ||
const found = currItem.find((item) => item.text === parts[0]); | ||
if (!found) | ||
currItem.push({ | ||
text: parts[0], | ||
items: [], | ||
link: path.join(dir, item), | ||
}); | ||
} | ||
return arr; | ||
} else { | ||
console.warn("No files found in the directory"); | ||
return []; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Packages | ||
|
||
- [`@opengovsg/starter-kitty-validators`](./validators.md): Common input validators. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# @opengovsg/starter-kitty-validators | ||
|
||
## Installation | ||
|
||
```bash | ||
npm i --save @opengovsg/starter-kitty-validators | ||
``` | ||
|
||
### Example | ||
|
||
```javascript | ||
import { UrlValidator } from '@opengovsg/starter-kitty-validators' | ||
|
||
const validator = new UrlValidator({ | ||
whitelist: { | ||
protocols: ['http', 'https', 'mailto'], | ||
hosts: ['open.gov.sg'], | ||
}, | ||
}) | ||
``` | ||
|
||
Validating a post-login redirect URL provided in a query parameter: | ||
|
||
```javascript | ||
try { | ||
router.push(validator.parse(redirectUrl)) | ||
} catch (error) { | ||
router.push('/home') | ||
} | ||
``` | ||
|
||
Consider using the validator as part of a Zod schema to validate the URL and fall back to a default URL if the URL is invalid. | ||
|
||
```javascript | ||
const baseUrl = getBaseUrl() | ||
|
||
const validator = new UrlValidator({ | ||
baseOrigin: new URL(baseUrl).origin, | ||
whitelist: { | ||
protocols: ['http', 'https'], | ||
hosts: [new URL(baseUrl).host], | ||
}, | ||
}) | ||
|
||
export const callbackUrlSchema = z | ||
.string() | ||
.optional() | ||
.default(HOME) | ||
.transform((url, ctx) => { | ||
try { | ||
return validator.parse(url) | ||
} catch (error) { | ||
ctx.addIssue({ | ||
code: z.ZodIssueCode.custom, | ||
message: (error as Error).message, | ||
}) | ||
return z.NEVER | ||
} | ||
}) | ||
.catch(new URL(HOME, baseUrl)) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters