Skip to content

Commit

Permalink
chore: broken ts
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Jun 12, 2024
1 parent 7b46151 commit 6e8cbd2
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/plugins/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { SourceMapInput } from 'rollup'
import type { Node } from 'estree-walker'
import { walk } from 'estree-walker'
import type { Literal, ObjectExpression, Property, SimpleCallExpression } from 'estree'
import type { Input } from 'valibot'
import type { InferInput } from 'valibot'
import type { RegistryScript } from '#nuxt-scripts'

export interface AssetBundlerTransformerOptions {
Expand Down Expand Up @@ -102,7 +102,7 @@ export function NuxtScriptBundleTransformer(options: AssetBundlerTransformerOpti
scriptSrcNode = srcProperty?.value as Literal & { start: number, end: number }
}
else {
src = registryNode.scriptBundling && registryNode.scriptBundling(fnArg0 as any as Input<any>)
src = registryNode.scriptBundling && registryNode.scriptBundling(fnArg0 as any as InferInput<any>)
// not supported
if (src === false)
return
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/registry/cloudflare-web-analytics.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useRegistryScript } from '../utils'
import { boolean, minLength, object, optional, string } from '#nuxt-scripts-validator'
import { boolean, minLength, object, optional, pipe, string } from '#nuxt-scripts-validator'
import type { RegistryScriptInput } from '#nuxt-scripts'

/**
Expand All @@ -25,7 +25,7 @@ export const CloudflareWebAnalyticsOptions = object({
/**
* The Cloudflare Web Analytics token.
*/
token: string([minLength(32)]),
token: pipe(string(), minLength(32)),
/**
* Cloudflare Web Analytics enables measuring SPAs automatically by overriding the History API’s pushState function
* and listening to the onpopstate. Hash-based router is not supported.
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/registry/intercom.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { joinURL } from 'ufo'
import { useRegistryScript } from '../utils'
import { type Input, literal, number, object, optional, string, union } from '#nuxt-scripts-validator'
import { type InferInput, literal, number, object, optional, string, union } from '#nuxt-scripts-validator'
import type { RegistryScriptInput } from '#nuxt-scripts'

export const IntercomOptions = object({
Expand All @@ -18,9 +18,9 @@ export const IntercomOptions = object({
export type IntercomInput = RegistryScriptInput<typeof IntercomOptions>

export interface IntercomApi {
Intercom: ((event: 'boot', data?: Input<typeof IntercomOptions>) => void)
Intercom: ((event: 'boot', data?: InferInput<typeof IntercomOptions>) => void)
& ((event: 'shutdown') => void)
& ((event: 'update', options?: Input<typeof IntercomOptions>) => void)
& ((event: 'update', options?: InferInput<typeof IntercomOptions>) => void)
& ((event: 'hide') => void)
& ((event: 'show') => void)
& ((event: 'showSpace', spaceName: 'home' | 'messages' | 'help' | 'news' | 'tasks' | 'tickets' | string) => void)
Expand Down
10 changes: 6 additions & 4 deletions src/runtime/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { UseScriptOptions } from '@unhead/schema'
import type { UseScriptInput, VueScriptInstance } from '@unhead/vue'
import type { ComputedRef, Ref } from 'vue'
import type { Input, ObjectSchema } from 'valibot'
import type { InferInput, ObjectSchema } from 'valibot'
import type { Import } from 'unimport'
import { object } from 'valibot'
import type { SegmentInput } from './registry/segment'
Expand All @@ -19,8 +19,10 @@ import type { YouTubePlayerInput } from './registry/youtube-player'
import type { PlausibleAnalyticsInput } from './registry/plausible-analytics'
import type { NpmInput } from './registry/npm'
import type { LemonSqueezyInput } from './registry/lemon-squeezy'
import type { Input as GoogleTagManagerInput } from '#build/nuxt-scripts/tpc/google-tag-manager'
import type { Input as GoogleAnalyticsInput } from '#build/nuxt-scripts/tpc/google-analytics'
// @ts-expect-error build-time
import type { InferInput as GoogleTagManagerInput } from '#build/nuxt-scripts/tpc/google-tag-manager'
// @ts-expect-error build-time
import type { InferInput as GoogleAnalyticsInput } from '#build/nuxt-scripts/tpc/google-analytics'

export type NuxtUseScriptOptions<T = any> = Omit<UseScriptOptions<T>, 'trigger'> & {
/**
Expand Down Expand Up @@ -110,7 +112,7 @@ const emptyOptions = object({})

export type EmptyOptionsSchema = typeof emptyOptions

export type RegistryScriptInput<T extends ObjectSchema<any> = EmptyOptionsSchema, Bundelable extends boolean = true> = Input<T> & {
export type RegistryScriptInput<T extends ObjectSchema<any, any> = EmptyOptionsSchema, Bundelable extends boolean = true> = InferInput<T> & {
scriptInput?: UseScriptInput
scriptOptions?: Bundelable extends true ? Omit<NuxtUseScriptOptions, 'use'> : Omit<NuxtUseScriptOptions, 'bundle' | 'use'>
}
Expand Down
11 changes: 6 additions & 5 deletions src/runtime/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defu } from 'defu'
import type { BaseSchema, Input, ObjectSchema, ValiError } from 'valibot'
import type { GenericSchema, InferInput, ObjectSchema, ValiError} from 'valibot'

Check failure on line 2 in src/runtime/utils.ts

View workflow job for this annotation

GitHub Actions / ci

A space is required before '}'
import type { UseScriptInput, VueScriptInstance } from '@unhead/vue'
import { parse } from '#nuxt-scripts-validator'
import { useRuntimeConfig, useScript } from '#imports'
Expand All @@ -10,20 +10,21 @@ import type {
ScriptRegistry,
} from '#nuxt-scripts'

function validateScriptInputSchema<T extends BaseSchema<any>>(key: string, schema: T, options?: Input<T>) {
function validateScriptInputSchema<T extends GenericSchema>(key: string, schema: T, options?: InferInput<T>) {
if (import.meta.dev) {
try {
parse(schema, options)
}
catch (_e) {
const e = _e as ValiError
const e = _e as ValiError<any>
// TODO nicer error handling
// @ts-expect-error untyped?
console.error(e.issues.map(i => `${key}.${i.path?.map(i => i.key).join(',')}: ${i.message}`).join('\n'))
}
}
}

type OptionsFn<O extends ObjectSchema<any>> = (options: Input<O>) => ({
type OptionsFn<O extends ObjectSchema<any, any>> = (options: InferInput<O>) => ({
scriptInput?: UseScriptInput
scriptOptions?: NuxtUseScriptOptions
schema?: O
Expand All @@ -34,7 +35,7 @@ export function scriptRuntimeConfig<T extends keyof ScriptRegistry>(key: T) {
return ((useRuntimeConfig().public.scripts || {}) as ScriptRegistry)[key]
}

export function useRegistryScript<T extends Record<string | symbol, any>, O extends ObjectSchema<any> = EmptyOptionsSchema>(key: keyof ScriptRegistry | string, optionsFn: OptionsFn<O>, _userOptions?: RegistryScriptInput<O>): T & {
export function useRegistryScript<T extends Record<string | symbol, any>, O extends ObjectSchema<any, any> = EmptyOptionsSchema>(key: keyof ScriptRegistry | string, optionsFn: OptionsFn<O>, _userOptions?: RegistryScriptInput<O>): T & {
$script: Promise<T> & VueScriptInstance<T>
} {
const scriptConfig = scriptRuntimeConfig(key as keyof ScriptRegistry)
Expand Down

0 comments on commit 6e8cbd2

Please sign in to comment.