Skip to content

Commit 1acc440

Browse files
committed
fix: module build
1 parent f9c311d commit 1acc440

File tree

5 files changed

+37
-9
lines changed

5 files changed

+37
-9
lines changed

Diff for: package.json

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
"name": "Sandro Circi",
1515
"email": "[email protected]"
1616
},
17+
"build": {
18+
"externals": [
19+
"defu",
20+
"ufo"
21+
]
22+
},
1723
"exports": {
1824
".": {
1925
"types": "./dist/types.d.ts",

Diff for: src/runtime/composables/surreal-db.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import type { AsyncDataOptions, AsyncData, NuxtError } from 'nuxt/app'
2+
import type { FetchError } from 'ofetch'
13
import { joinURL } from 'ufo'
24
import { hash } from 'ohash'
35

4-
import type { Overrides } from '../types'
5-
import type { AsyncDataOptions } from '#app'
6+
import type { Overrides, PickFrom, KeysOf } from '../types'
67
import type { MaybeRefOrGetter } from '#imports'
78
import { computed, createError, toValue, useAsyncData, useNuxtApp, useSurrealFetch } from '#imports'
89

@@ -26,7 +27,7 @@ export function useSurrealDB(overrides?: Overrides) {
2627
method: 'GET',
2728
key: undefined,
2829
},
29-
) {
30+
): Promise<AsyncData<PickFrom<T, KeysOf<T>> | null, NuxtError<unknown> | null>> {
3031
const _record = toValue(record)
3132
const {
3233
key,
@@ -77,7 +78,7 @@ export function useSurrealDB(overrides?: Overrides) {
7778
// TODO: POST /signup Signs-up as a scope user to a specific scope
7879
// TODO: POST /signin Signs-in as a root, namespace, database, or scope user
7980

80-
async function sql<T = any>(sql: string, ovr?: Overrides) {
81+
async function sql<T = any>(sql: string, ovr?: Overrides): Promise<AsyncData<PickFrom<T, KeysOf<T>> | null, FetchError<any> | null>> {
8182
return useSurrealFetch<T>('sql', {
8283
...(ovr || overrides),
8384
method: 'POST',
@@ -93,7 +94,7 @@ export function useSurrealDB(overrides?: Overrides) {
9394
})
9495
}
9596

96-
async function version(ovr?: Overrides) {
97+
async function version(ovr?: Overrides): Promise<AsyncData<any, FetchError<any> | null>> {
9798
return useSurrealFetch('version', {
9899
...$surrealFetchOptionsOverride(ovr || overrides),
99100
})

Diff for: src/runtime/composables/surreal-fetch.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import type { UseFetchOptions } from 'nuxt/app'
1+
import { useFetch, useNuxtApp, useRuntimeConfig } from 'nuxt/app'
2+
import type { AsyncData, UseFetchOptions } from 'nuxt/app'
23
import { type MaybeRefOrGetter, ref } from 'vue'
4+
import type { FetchError } from 'ofetch'
35

4-
import type { DatabasePreset, Overrides } from '../types'
5-
import { useFetch, useNuxtApp, useRuntimeConfig } from '#app'
6+
import type { DatabasePreset, Overrides, PickFrom, KeysOf } from '../types'
67

78
export function useSurrealFetch<T = any>(
89
endpoint: MaybeRefOrGetter<string>,
910
options: UseFetchOptions<T> & Overrides = {},
10-
) {
11+
): AsyncData<PickFrom<T, KeysOf<T>> | null, FetchError<any> | null> {
1112
const {
1213
database,
1314
token,

Diff for: src/runtime/plugin.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// The following nitropack import is from https://github.com/nuxt/module-builder/issues/141#issuecomment-2078248248
2+
import type {} from 'nitropack'
13
import type { FetchOptions, ResponseType } from 'ofetch'
24

35
import type { DatabasePreset, Overrides } from './types'

Diff for: src/runtime/types/index.d.ts

+18
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,21 @@ type ErrorResponse = {
2525

2626
export type Response<T> = Array<(OKResponse<T> | ErrorResponse)>
2727
export type Res<T> = Response<T>
28+
29+
export type PickFrom<T, K extends Array<string>> = T extends Array<any>
30+
? T
31+
: T extends Record<string, any>
32+
? keyof T extends K[number]
33+
? T // Exact same keys as the target, skip Pick
34+
: K[number] extends never
35+
? T
36+
: Pick<T, K[number]>
37+
: T
38+
39+
export type KeysOf<T> = Array<
40+
T extends T // Include all keys of union types, not just common keys
41+
? keyof T extends string
42+
? keyof T
43+
: never
44+
: never
45+
>

0 commit comments

Comments
 (0)