Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace 'any' with 'unknown' to avoid certain typescript pitfalls #18

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
interface ComponentSettings {
[key: string]: any
[key: string]: unknown
}

interface ClientSetOptions {
Expand All @@ -15,7 +15,7 @@ type WidgetCallback = () => Promise<string>

interface MCEvent {
readonly name?: string
readonly payload: any
readonly payload: Record<string, unknown | undefined>
client: Client
readonly type: string
}
Expand Down Expand Up @@ -54,19 +54,22 @@ interface Manager {
type: ClientEventType,
callback: MCEventListener
): boolean | undefined
get(key: string): Promise<any>
set(key: string, value: any): Promise<boolean>
get(key: string): Promise<string | null>
set(key: string, value: string): Promise<boolean>
route(
path: string,
callback: (request: Request | any) => Promise<Response> | Response
): string | undefined
proxy(path: string, target: string): string | undefined
serve(path: string, target: string): string | undefined
useCache(key: string, callback: Function, expiry?: number): Promise<any>
useCache(key: string, callback: Function, expiry?: number): Promise<string>
invalidateCache(key: string): Promise<void>
registerEmbed(name: string, callback: EmbedCallback): boolean | undefined
registerWidget(callback: WidgetCallback): boolean | undefined
fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response> | undefined
fetch(
input: RequestInfo | URL,
init?: RequestInit
): Promise<Response> | undefined
}

interface Client {
Expand All @@ -86,7 +89,7 @@ interface Client {

fetch(resource: string, settings?: RequestInit): boolean | undefined
execute(code: string): boolean | undefined
return(value: unknown): void
return(value: any): void
set(
key: string,
value?: string | null,
Expand All @@ -109,7 +112,7 @@ type Permission =
| 'client_network_requests'
| 'serve_static_files'
| 'provide_server_functionality'
| "server_network_requests"
| 'server_network_requests'

export {
ComponentSettings,
Expand Down
10 changes: 9 additions & 1 deletion tests/demoComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export default async function (manager: Manager, settings: ComponentSettings) {
// Save mouse coordinates as a cookie
const { client, payload } = event
console.info('🐁 ⬇️ Mousedown payload:', payload)
if (!Array.isArray(payload.mousedown)) {
throw new Error('expected mousedown to be an array')
}
const [firstClick] = payload.mousedown
client.set('lastClickX', firstClick.clientX)
client.set('lastClickY', firstClick.clientY)
Expand Down Expand Up @@ -85,7 +88,9 @@ export default async function (manager: Manager, settings: ComponentSettings) {
payload.user_id = client.get('user_id')

if (Object.keys(payload || {}).length) {
const params = new URLSearchParams(payload).toString()
const params = new URLSearchParams(
payload as Record<string, string>
).toString()
fetch(`http://www.example.com/?${params}`)
}
})
Expand Down Expand Up @@ -129,6 +134,9 @@ export default async function (manager: Manager, settings: ComponentSettings) {
console.error('error fetching weather for embed:', error)
}
})
if (typeof embed !== 'string') {
throw new Error('Unexpected embed return type: ' + typeof embed)
}
return embed
}
)
Expand Down
Loading