Skip to content

Commit

Permalink
chore: enable perfectionist ESLint plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Nov 18, 2024
1 parent f36722a commit dbf0dbb
Show file tree
Hide file tree
Showing 17 changed files with 193 additions and 90 deletions.
2 changes: 1 addition & 1 deletion docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineConfig } from 'vitepress'
import type { DefaultTheme } from 'vitepress'
import UnoCSS from 'unocss/vite'
import { defineConfig } from 'vitepress'
import { description, version } from '../../package.json'
import { github, name, ogImage, ogUrl, releases } from './meta'

Expand Down
29 changes: 28 additions & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-check
import { createConfigForNuxt } from '@nuxt/eslint-config/flat'
import perfectionist from 'eslint-plugin-perfectionist'

export default createConfigForNuxt({
features: {
Expand All @@ -12,3 +12,30 @@ export default createConfigForNuxt({
],
},
})
.append({
plugins: {
perfectionist,
},
rules: {
'import/order': 'off',
'perfectionist/sort-exports': ['error', { order: 'asc', type: 'natural' }],
'perfectionist/sort-imports': ['error', {
groups: [
'type',
['parent-type', 'sibling-type', 'index-type'],
'builtin',
'external',
['internal', 'internal-type'],
['parent', 'sibling', 'index'],
'side-effect',
'object',
'unknown',
],
newlinesBetween: 'ignore',
order: 'asc',
type: 'natural',
}],
'perfectionist/sort-named-exports': ['error', { order: 'asc', type: 'natural' }],
'perfectionist/sort-named-imports': ['error', { order: 'asc', type: 'natural' }],
},
})
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"@types/node": "^22.9.0",
"bumpp": "^9.8.1",
"eslint": "^9.14.0",
"eslint-plugin-perfectionist": "^3.9.1",
"nuxt": "^3.14.159",
"openapi-typescript": "^7.4.3",
"typescript": "^5.6.3",
Expand Down
2 changes: 1 addition & 1 deletion playground/pages/jsonPlaceholder.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts">
import type { NuxtError } from '#app'
import type { JsonPlaceholderComment } from '../types'
import { $jsonPlaceholder, computed, navigateTo, ref, useJsonPlaceholderData, useRoute, watch } from '#imports'
import type { NuxtError } from '#app'
const route = useRoute()
Expand Down
175 changes: 125 additions & 50 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/kit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { relative } from 'pathe'
import { addTemplate, createResolver, useNuxt } from '@nuxt/kit'
import { relative } from 'pathe'

export function extendTypes(
module: string,
Expand Down
14 changes: 7 additions & 7 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { relative } from 'pathe'
import { defu } from 'defu'
import { joinURL } from 'ufo'
import { camelCase, pascalCase } from 'scule'
import { createJiti } from 'jiti'
import { addImportsSources, addServerHandler, addTemplate, createResolver, defineNuxtModule, useLogger } from '@nuxt/kit'
import type { HookResult } from '@nuxt/schema'
import type { OpenAPI3, OpenAPITSOptions } from 'openapi-typescript'
import type { QueryObject } from 'ufo'
import { addImportsSources, addServerHandler, addTemplate, createResolver, defineNuxtModule, useLogger } from '@nuxt/kit'
import { defu } from 'defu'
import { createJiti } from 'jiti'
import { relative } from 'pathe'
import { camelCase, pascalCase } from 'scule'
import { joinURL } from 'ufo'
import { name } from '../package.json'
import { generateDeclarationTypes } from './openapi'

Expand Down Expand Up @@ -181,8 +181,8 @@ export default defineNuxtModule<ModuleOptions>({
// Add Nuxt server route to proxy the API request server-side
addServerHandler({
route: joinURL('/api', options.server!.basePath!, ':endpointId'),
method: 'post',
handler: resolve('runtime/server/handler'),
method: 'post',
})

nuxt.hook('nitro:config', (config) => {
Expand Down
4 changes: 2 additions & 2 deletions src/openapi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { resolve } from 'pathe'
import { useNuxt } from '@nuxt/kit'
import type { OpenAPI3, OpenAPITSOptions } from 'openapi-typescript'
import type { EndpointConfiguration } from './module'
import { useNuxt } from '@nuxt/kit'
import { resolve } from 'pathe'

/** @deprecated Hooks should be used instead */
type SchemaFn = () => Promise<NonNullable<EndpointConfiguration['schema']>>
Expand Down
14 changes: 7 additions & 7 deletions src/runtime/composables/$api.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { joinURL } from 'ufo'
import { hash } from 'ohash'
import type { NitroFetchOptions } from 'nitropack'
import { resolvePathParams } from '../openapi'
import { headersToObject, serializeMaybeEncodedBody } from '../utils'
import { isFormData } from '../form-data'
import type { ModuleOptions } from '../../module'
import { CACHE_KEY_PREFIX } from '../constants'
import type { EndpointFetchOptions } from '../types'
import type { FetchResponseData, FilterMethods, MethodOption, ParamsOption, RequestBodyOption } from '../openapi'
import type { EndpointFetchOptions } from '../types'
import { useNuxtApp, useRequestHeaders, useRuntimeConfig } from '#imports'
import { hash } from 'ohash'
import { joinURL } from 'ufo'
import { CACHE_KEY_PREFIX } from '../constants'
import { isFormData } from '../form-data'
import { resolvePathParams } from '../openapi'
import { headersToObject, serializeMaybeEncodedBody } from '../utils'

export interface SharedFetchOptions {
/**
Expand Down
16 changes: 8 additions & 8 deletions src/runtime/composables/useApiData.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { computed, reactive, toValue } from 'vue'
import { hash } from 'ohash'
import { joinURL } from 'ufo'
import type { NitroFetchOptions } from 'nitropack'
import type { MaybeRef, MaybeRefOrGetter, MultiWatchSources } from 'vue'
import type { AsyncData, AsyncDataOptions, NuxtError } from 'nuxt/app'
import type { MaybeRef, MaybeRefOrGetter, MultiWatchSources } from 'vue'
import type { ModuleOptions } from '../../module'
import type { FetchResponseData, FetchResponseError, FilterMethods, ParamsOption, RequestBodyOption } from '../openapi'
import type { EndpointFetchOptions } from '../types'
import { useAsyncData, useRequestHeaders, useRuntimeConfig } from '#imports'
import { hash } from 'ohash'
import { joinURL } from 'ufo'
import { computed, reactive, toValue } from 'vue'
import { CACHE_KEY_PREFIX } from '../constants'
import { isFormData } from '../form-data'
import { resolvePathParams } from '../openapi'
import { headersToObject, serializeMaybeEncodedBody } from '../utils'
import { isFormData } from '../form-data'
import type { EndpointFetchOptions } from '../types'
import type { FetchResponseData, FetchResponseError, FilterMethods, ParamsOption, RequestBodyOption } from '../openapi'
import { useAsyncData, useRequestHeaders, useRuntimeConfig } from '#imports'

type ComputedOptions<T> = {
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/openapi.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { toValue } from 'vue'
import type { MaybeRefOrGetter } from 'vue'
import type { NuxtError } from 'nuxt/app'
import type {
ErrorResponse,
IsOperationRequestBodyOptional,
MediaType,
OperationRequestBodyContent,
ResponseObjectMap,
IsOperationRequestBodyOptional,
SuccessResponse,
} from 'openapi-typescript-helpers'
import type { MaybeRefOrGetter } from 'vue'
import { toValue } from 'vue'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type FetchResponseData<T extends Record<PropertyKey, any>> = SuccessResponse<ResponseObjectMap<T>, MediaType>
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/server/$api.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { headersToObject } from '../utils'
import { resolvePathParams } from '../openapi'
import type { ModuleOptions } from '../../module'
import type { ApiClientFetchOptions } from '../composables/$api'
import { useRuntimeConfig } from '#imports'
import { resolvePathParams } from '../openapi'
import { headersToObject } from '../utils'

export function _$api<T = unknown>(
endpointId: string,
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/server/handler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import type { ModuleOptions } from '../../module'
import type { EndpointFetchOptions } from '../types'
import { useRuntimeConfig } from '#imports'
import {
createError,
defineEventHandler,
Expand All @@ -10,9 +13,6 @@ import {
splitCookiesString,
} from 'h3'
import { deserializeMaybeEncodedBody } from '../utils'
import type { ModuleOptions } from '../../module'
import type { EndpointFetchOptions } from '../types'
import { useRuntimeConfig } from '#imports'

export default defineEventHandler(async (event) => {
const endpointId = getRouterParam(event, 'endpointId')!
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { NitroFetchOptions } from 'nitropack'
import type { RouterMethod } from 'h3'
import type { NitroFetchOptions } from 'nitropack'

export type EndpointFetchOptions = NitroFetchOptions<string> & {
path: string
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { formDataToObject, isFormData, isSerializedFormData, objectToFormData } from './form-data'
import type { ApiClientFetchOptions } from './composables/$api'
import { formDataToObject, isFormData, isSerializedFormData, objectToFormData } from './form-data'

export function headersToObject(headers: HeadersInit = {}): Record<string, string> {
return Object.fromEntries(new Headers(headers))
Expand Down
2 changes: 1 addition & 1 deletion test/e2e.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fileURLToPath } from 'node:url'
import { $fetch, setup } from '@nuxt/test-utils/e2e'
import destr from 'destr'
import { describe, expect, it } from 'vitest'
import { $fetch, setup } from '@nuxt/test-utils/e2e'

describe('nuxt-api-party', async () => {
await setup({
Expand Down
2 changes: 1 addition & 1 deletion test/fixture/pages/$testApi-error.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
import { $testApi, useTestResult } from '#imports'
import type { NuxtError } from '#app'
import { $testApi, useTestResult } from '#imports'
try {
await $testApi('foo/bar', {
Expand Down

0 comments on commit dbf0dbb

Please sign in to comment.