Skip to content

Commit

Permalink
feat: manual client usage
Browse files Browse the repository at this point in the history
  • Loading branch information
wobsoriano committed Apr 4, 2023
1 parent 914b441 commit e8cce4a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default defineNuxtModule<ModuleOptions>({
await scanRemoteFunctions()

addTemplate({
filename: 'remote-handler',
filename: 'remote-handler.ts',
write: true,
getContents () {
const filesWithId = files.map(file => ({
Expand All @@ -66,6 +66,10 @@ export default defineNuxtModule<ModuleOptions>({
import { createRemoteFnHandler } from ${JSON.stringify(resolver.resolve(runtimeDir, 'server'))}
${filesWithId.map(i => `import * as ${i.id} from ${JSON.stringify(i.file)}`).join('\n')}
export type RemoteFunction = {
${filesWithId.map(i => `${i.id}: typeof ${i.id}`).join('\n')}
}
export default createRemoteFnHandler({
${filesWithId.map(i => i.id).join(',\n')}
})
Expand Down
9 changes: 7 additions & 2 deletions src/runtime/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export function createClient<T>() {
interface Options {
fetchOptions: Parameters<typeof globalThis.$fetch>[1]
}

export function createClient<T>(options?: Options) {
function generateAPI(baseUrl = '/api/__remote'): T {
const noop = () => {}
noop.url = baseUrl
Expand All @@ -9,10 +13,11 @@ export function createClient<T>() {
},
apply({ url }, _thisArg, args) {
return globalThis.$fetch(url, {
...options?.fetchOptions,
method: 'POST',
body: {
args
}
},
})
},
}) as unknown as T
Expand Down

0 comments on commit e8cce4a

Please sign in to comment.