Skip to content

Commit

Permalink
refactor: split runtime types (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Jan 14, 2022
1 parent eaf21fa commit 6fab1ce
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 53 deletions.
46 changes: 38 additions & 8 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,41 @@ import defu from 'defu'
import { resolve } from 'pathe'
import { defineNuxtModule, addPlugin } from '@nuxt/kit'
import type { Nuxt } from '@nuxt/schema'
import type { StrapiOptions } from './types'
import type { CookieOptions } from 'nuxt3/dist/app/composables/cookie'

export default defineNuxtModule<StrapiOptions>({
export interface ModuleOptions {
/**
* Strapi API URL
* @default process.env.STRAPI_URL
* @example 'http://localhost:1337'
* @type string
*/
url?: string

/**
* Strapi Prefix
* @default '/api'
* @type string
*/
prefix?: string

/**
* Strapi Version
* @default 'v4'
* @type string
* @example 'v3'
*/
version?: 'v4' | 'v4'

/**
* Nuxt Cookie Options
* @default {}
* @type CookieOptions
*/
cookie?: CookieOptions
}

export default defineNuxtModule<ModuleOptions>({
meta: {
name: '@nuxtjs/strapi',
configKey: 'strapi',
Expand All @@ -19,7 +51,7 @@ export default defineNuxtModule<StrapiOptions>({
version: 'v4',
cookie: {}
},
setup (options: StrapiOptions, nuxt: Nuxt) {
setup (options: ModuleOptions, nuxt: Nuxt) {
// Make sure url is set
if (!options.url) {
throw new Error('Missing `STRAPI_URL` in `.env`')
Expand Down Expand Up @@ -47,18 +79,16 @@ export default defineNuxtModule<StrapiOptions>({
}
})

export * from './types'

declare module '@nuxt/schema' {
interface ConfigSchema {
publicRuntimeConfig?: {
strapi?: StrapiOptions
strapi?: ModuleOptions
}
}
interface NuxtConfig {
strapi?: StrapiOptions
strapi?: ModuleOptions
}
interface NuxtOptions {
strapi?: StrapiOptions
strapi?: ModuleOptions
}
}
2 changes: 1 addition & 1 deletion src/runtime/composables/useStrapi3.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Strapi3RequestParams } from '../../types/v3'
import type { Strapi3RequestParams } from '../types/v3'
import { useStrapiVersion } from './useStrapiVersion'
import { useStrapiClient } from './useStrapiClient'

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/composables/useStrapi4.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Strapi4RequestParams } from '../../types/v4'
import type { Strapi4RequestParams } from '../types/v4'
import { useStrapiVersion } from './useStrapiVersion'
import { useStrapiClient } from './useStrapiClient'

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/composables/useStrapiAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
StrapiRegistrationData,
StrapiResetPasswordData,
StrapiUser
} from '../../types'
} from '../types'
import { useStrapiToken } from './useStrapiToken'
import { useStrapiUser } from './useStrapiUser'
import { useStrapiClient } from './useStrapiClient'
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/composables/useStrapiClient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { FetchError, FetchOptions } from 'ohmyfetch'
import { stringify } from 'qs'
import type { Strapi4Error } from '../../types/v4'
import type { Strapi3Error } from '../../types/v3'
import type { Strapi4Error } from '../types/v4'
import type { Strapi3Error } from '../types/v3'
import { useStrapiUrl } from './useStrapiUrl'
import { useStrapiVersion } from './useStrapiVersion'
import { useStrapiToken } from './useStrapiToken'
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/composables/useStrapiUser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Ref } from 'vue'
import type { StrapiUser } from '../../types'
import type { StrapiUser } from '../types'
import { useState } from '#app'

export const useStrapiUser = (): Ref<StrapiUser> => useState<StrapiUser>('strapi_user')
2 changes: 1 addition & 1 deletion src/runtime/composables/useStrapiVersion.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { StrapiOptionsVersion } from '../../types'
import type { StrapiOptionsVersion } from '../types'
import { useRuntimeConfig } from '#app'

export const useStrapiVersion = (): StrapiOptionsVersion => {
Expand Down
36 changes: 0 additions & 36 deletions src/types/index.ts → src/runtime/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { CookieOptions } from 'nuxt3/dist/app/composables/cookie'
import type { Ref } from 'vue'

export type StrapiLocale =
Expand Down Expand Up @@ -517,41 +516,6 @@ export type StrapiAuthProvider =
| 'reddit'
| 'auth0'

export type StrapiOptionsVersion =
| 'v4'
| 'v3'

export interface StrapiOptions {
/**
* Strapi API URL
* @default process.env.STRAPI_URL
* @example 'http://localhost:1337'
* @type string
*/
url?: string

/**
* Strapi Prefix
* @default '/api'
* @type string
*/
prefix?: string

/**
* Strapi Version
* @default 'v4'
* @type string
* @example 'v3'
*/
version?: StrapiOptionsVersion

/**
* Nuxt Cookie Options
* @default {}
* @type CookieOptions
*/
cookie?: CookieOptions
}

export type StrapiUser = object | null

Expand Down
2 changes: 1 addition & 1 deletion src/types/v3.ts → src/runtime/types/v3.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { StrapiLocale } from '../types'
import type { StrapiLocale } from '.'

export interface Strapi3Error {
error: string
Expand Down
2 changes: 1 addition & 1 deletion src/types/v4.ts → src/runtime/types/v4.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { StrapiLocale } from '../types'
import type { StrapiLocale } from '.'

export interface Strapi4Error {
error: {
Expand Down

0 comments on commit 6fab1ce

Please sign in to comment.