Skip to content

Commit

Permalink
feat: typescript support (#2)
Browse files Browse the repository at this point in the history
* feat: typescript support

* test: fix test
  • Loading branch information
climba03003 committed Oct 29, 2021
1 parent b3ab1d8 commit 289827b
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .taprc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ts: false
jsx: false
flow: false
check-coverage: true
coverage: true
3 changes: 0 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
{
"files.exclude": {
"**/*.d.ts": { "when": "$(basename).ts" },
"**/*.js.map": { "when": "$(basename)" },
"**/*.js": { "when": "$(basename).ts" },
"node_modules": true
},
"editor.formatOnSave": true,
Expand Down
26 changes: 26 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export interface SecureURLOption {
mode?: 'path' | 'relax' | 'insecure'
keepPort?: boolean
}

export declare class SecureURL {
constructor(path: string | SecureURL | URL, option?: SecureURLOption)
constructor(path: string | SecureURL | URL, base?: string | SecureURL | URL, option?: SecureURLOption)

readonly href: string
readonly origin: string
readonly host: string
readonly protocol: string
readonly username: string
readonly password: string
readonly hostname: string
readonly port?: number
readonly pathname: string
readonly hash: string
readonly search: string
readonly searchParams: URLSearchParams
}

export type URL = SecureURL

export default SecureURL
15 changes: 11 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
"name": "secure-url",
"version": "0.1.0",
"description": "",
"main": "index.js",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"directories": {
"lib": "lib"
},
"scripts": {
"lint": "standard",
"lint:fix": "npm run lint -- --fix",
"unit": "tap",
"test": "npm run lint && npm run unit",
"unit": "tap test/*.test.js",
"unit:typescript": "tsd",
"test": "npm run lint && npm run unit && npm run unit:typescript",
"coverage": "tap --coverage --coverage-report=lcovonly"
},
"publishConfig": {
Expand All @@ -25,6 +27,11 @@
"license": "MIT",
"devDependencies": {
"standard": "^16.0.4",
"tap": "^15.0.10"
"tap": "^15.0.10",
"tsd": "^0.18.0",
"typescript": "^4.4.4"
},
"tsd": {
"directory": "test"
}
}
44 changes: 44 additions & 0 deletions test/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { expectAssignable, expectError, expectType } from 'tsd'
import DefaultExport, { SecureURL, SecureURLOption } from '../lib'

// default export and named export should be the same
expectType<DefaultExport>(new SecureURL(''))
expectType<SecureURL>(new DefaultExport(''))

// option
expectAssignable<SecureURLOption>({})
expectAssignable<SecureURLOption>({ mode: 'path' })
expectAssignable<SecureURLOption>({ mode: 'relax' })
expectAssignable<SecureURLOption>({ mode: 'insecure' })
expectAssignable<SecureURLOption>({ keepPort: true })
expectAssignable<SecureURLOption>({ keepPort: false })
expectAssignable<SecureURLOption>({ mode: 'path', keepPort: true })

// two arguments
expectType<SecureURL>(new SecureURL('', ''))
expectType<SecureURL>(new SecureURL('', {}))
expectType<SecureURL>(new SecureURL('', { mode: 'path', keepPort: true }))

// three arguments
expectType<SecureURL>(new SecureURL('', '', {}))
expectType<SecureURL>(new SecureURL('', '', { mode: 'path', keepPort: true }))

// invalid argument
expectError(new SecureURL())
expectError(new SecureURL(true))
expectError(new SecureURL({}))
expectError(new SecureURL('', { mode: 'invalid' }))

const url = new SecureURL('https://localhost.local/')
expectType<string>(url.href)
expectType<string>(url.origin)
expectType<string>(url.host)
expectType<string>(url.protocol)
expectType<string>(url.username)
expectType<string>(url.password)
expectType<string>(url.hostname)
expectType<number | undefined>(url.port)
expectType<string>(url.pathname)
expectType<string>(url.hash)
expectType<string>(url.search)
expectType<URLSearchParams>(url.searchParams)

0 comments on commit 289827b

Please sign in to comment.