|
1 | | -import { Module } from '@nuxt/types' |
2 | | -import path from 'path' |
| 1 | +import { Module, NuxtOptions } from "@nuxt/types" |
| 2 | +import { ModuleMeta } from "@nuxt/schema" |
| 3 | +import path from "pathe" |
| 4 | +import type { ApiInstance } from "@privyid/nugrpc-api" |
| 5 | +import defu from "defu" |
3 | 6 |
|
4 | | -// export * from './runtime/api' |
5 | | -// export * from './error' |
6 | | - |
7 | | -const NugrpcApi: Module<void> = function () { |
8 | | - this.addPlugin({ src: path.resolve(__dirname, './runtime/api.mjs') }) |
9 | | - this.requireModule(['@nuxtjs/axios', { proxyHeaders: false }]) |
| 7 | +interface Options { |
| 8 | + baseURL: string; |
| 9 | + browserBaseURL: string; |
| 10 | + https: boolean; |
| 11 | + proxyHeaders: boolean; |
| 12 | + proxyHeadersIgnore: string[]; |
10 | 13 | } |
11 | 14 |
|
12 | | -export default NugrpcApi |
| 15 | +function toHttps (url: string): string { |
| 16 | + return url.replace('http://', 'https://') |
| 17 | +} |
13 | 18 |
|
14 | 19 | // REQUIRED if publishing the module as npm package |
15 | | -export const meta = { |
| 20 | +export const meta: ModuleMeta = { |
16 | 21 | name : '@privyid/nugrpc-api', |
17 | 22 | version : '1.0.0', |
18 | 23 | configKey: 'nugrpcApi', |
19 | 24 | } |
20 | | -// import { defineNuxtModule, installModule, addPlugin } from "@nuxt/kit" |
21 | | - |
22 | | -// export default defineNuxtModule({ |
23 | | -// meta: { |
24 | | -// name : '@privyid/nugrpc-api', |
25 | | -// version: '1.0.0', |
26 | | -// }, |
27 | | -// setup () { |
28 | | -// addPlugin({ src: path.resolve(__dirname, './runtime/api.mjs') }) |
29 | | -// installModule('@nuxtjs/axios', { proxyHeaders: false }) |
30 | | -// } |
31 | | -// }) |
| 25 | + |
| 26 | +const NugrpcApi: Module<Partial<Options>> = function (_moduleOptions) { |
| 27 | + const nuxt = this.nuxt |
| 28 | + |
| 29 | + // Combine options |
| 30 | + const moduleOptions = { |
| 31 | + ...nuxt.options.axios, |
| 32 | + ..._moduleOptions, |
| 33 | + ...(nuxt.options.runtimeConfig && nuxt.options.runtimeConfig.axios) |
| 34 | + } |
| 35 | + |
| 36 | + const defaultPort = |
| 37 | + process.env.API_PORT || |
| 38 | + moduleOptions.port || |
| 39 | + process.env.PORT || |
| 40 | + process.env.NUXT_PORT || |
| 41 | + process.env.npm_package_config_nuxt_port || |
| 42 | + (this.options.server && this.options.server.port) || |
| 43 | + 3000 |
| 44 | + |
| 45 | + // Default host |
| 46 | + let defaultHost = |
| 47 | + process.env.API_HOST || |
| 48 | + moduleOptions.host || |
| 49 | + process.env.HOST || |
| 50 | + process.env.NUXT_HOST || |
| 51 | + process.env.npm_package_config_nuxt_host || |
| 52 | + (this.options.server && this.options.server.host) || |
| 53 | + 'localhost' |
| 54 | + |
| 55 | + /* istanbul ignore if */ |
| 56 | + if (defaultHost === '0.0.0.0') { |
| 57 | + defaultHost = 'localhost' |
| 58 | + } |
| 59 | + |
| 60 | + const https = Boolean(this.options.server && this.options.server.https) |
| 61 | + const options = defu(moduleOptions, { |
| 62 | + baseURL : `http://${defaultHost}:${defaultPort}`, |
| 63 | + browserBaseURL: undefined, |
| 64 | + https : https, |
| 65 | + }) |
| 66 | + |
| 67 | + // Convert http:// to https:// if https option is on |
| 68 | + if (options.https === true) { |
| 69 | + options.baseURL = toHttps(options.baseURL) |
| 70 | + options.browserBaseURL = toHttps(options.browserBaseURL) |
| 71 | + } |
| 72 | + |
| 73 | + this.addPlugin({ |
| 74 | + src : path.resolve(__dirname, './runtime/api.mjs'), |
| 75 | + fileName: 'axios.js', |
| 76 | + options : options, |
| 77 | + }) |
| 78 | + |
| 79 | + // Set _AXIOS_BASE_URL_ for dynamic SSR baseURL |
| 80 | + process.env._AXIOS_BASE_URL_ = options.baseURL |
| 81 | +} |
| 82 | + |
| 83 | +export default NugrpcApi |
| 84 | + |
| 85 | +declare module '@nuxt/types' { |
| 86 | + export interface Context { |
| 87 | + /** |
| 88 | + * Api instance attached to the app. |
| 89 | + */ |
| 90 | + $api: ApiInstance; |
| 91 | + } |
| 92 | + |
| 93 | + export interface NuxtConfig { |
| 94 | + nugrcpApi?: Partial<Options>; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +declare module '@nuxt/schema' { |
| 99 | + export interface NuxtConfig { |
| 100 | + nugrcpApi?: Partial<Options>; |
| 101 | + } |
| 102 | +} |
0 commit comments