Skip to content

Commit

Permalink
fix: move types to runtime entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Mar 31, 2021
1 parent c10a060 commit 4cbac01
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/components/sanity-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import defu from 'defu'
import type { CreateElement, VNode, VNodeData, VueConstructor } from 'vue'

// eslint-disable-next-line
import type { SanityConfiguration } from '../module'
import type { SanityConfiguration } from '..'
import { extendVue } from './vue'

interface MarkDefs {
Expand Down
2 changes: 1 addition & 1 deletion src/components/sanity-image.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { extendVue } from './vue'

// eslint-disable-next-line
import type { SanityConfiguration } from '../module'
import type { SanityConfiguration } from '..'

const baseURL = 'https://cdn.sanity.io/images'

Expand Down
42 changes: 42 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,44 @@
import type { SanityClient } from '@sanity/client'

import type { SanityModuleOptions, CONFIG_KEY } from './module'

import type { createClient } from './client'

const HELPER_KEY: `$${typeof CONFIG_KEY}` = '$sanity'

export * from './client'
export * from './helpers'

interface Client {
client: SanityClient
config: Pick<SanityModuleOptions, 'useCdn' | 'projectId' | 'dataset' | 'withCredentials' | 'token'>
fetch: ReturnType<typeof createClient>['fetch']
setToken: (token: string) => void
}

type SanityHelper = Record<string, Client> & Client

declare module '@nuxt/types' {
interface NuxtOptions {
[CONFIG_KEY]: SanityModuleOptions
} // Nuxt 2.14+
interface Configuration {
[CONFIG_KEY]: SanityModuleOptions
} // Nuxt 2.9 - 2.13
interface NuxtAppOptions {
[HELPER_KEY]: SanityHelper
}
}

declare module 'vue/types/vue' {
interface Vue {
[HELPER_KEY]: SanityHelper
}
}

declare module 'vuex/types/index' {
// eslint-disable-next-line
interface Store<S> {
[HELPER_KEY]: SanityHelper
}
}
42 changes: 2 additions & 40 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ import { readJSONSync } from 'fs-extra'
import { join, resolve } from 'upath'

import type { Module } from '@nuxt/types'
import type { SanityClient } from '@sanity/client'

import { name, version } from '../package.json'

import type { SanityConfiguration, createClient } from '.'

export type { SanityConfiguration }
import type { SanityConfiguration } from '.'

export interface SanityModuleOptions extends Partial<SanityConfiguration> {
/**
Expand Down Expand Up @@ -52,8 +49,7 @@ const DEFAULTS: SanityModuleOptions = {
additionalClients: {},
}

const CONFIG_KEY = 'sanity'
const HELPER_KEY = '$sanity'
export const CONFIG_KEY = 'sanity' as const

function validateConfig ({ projectId, dataset }: SanityModuleOptions) {
if (!projectId) {
Expand Down Expand Up @@ -152,38 +148,4 @@ const sanityModule: Module<SanityModuleOptions> = function sanityModule (moduleO

;(sanityModule as any).meta = { name, version }

interface Client {
client: SanityClient
config: Pick<SanityModuleOptions, 'useCdn' | 'projectId' | 'dataset' | 'withCredentials' | 'token'>
fetch: ReturnType<typeof createClient>['fetch']
setToken: (token: string) => void
}

type SanityHelper = Record<string, Client> & Client

declare module '@nuxt/types' {
interface NuxtOptions {
[CONFIG_KEY]: SanityModuleOptions
} // Nuxt 2.14+
interface Configuration {
[CONFIG_KEY]: SanityModuleOptions
} // Nuxt 2.9 - 2.13
interface NuxtAppOptions {
[HELPER_KEY]: SanityHelper
}
}

declare module 'vue/types/vue' {
interface Vue {
[HELPER_KEY]: SanityHelper
}
}

declare module 'vuex/types/index' {
// eslint-disable-next-line
interface Store<S> {
[HELPER_KEY]: SanityHelper
}
}

export default sanityModule

0 comments on commit 4cbac01

Please sign in to comment.