Skip to content

Commit 40d418f

Browse files
authored
Merge pull request #48 from privy-open-source/refactor/nugrpc-api
feat(nugrpc-api-nuxt): nugrpc-api-nuxt
2 parents 81c778b + f38c3ab commit 40d418f

File tree

37 files changed

+18145
-6568
lines changed

37 files changed

+18145
-6568
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"packageManager": "[email protected]",
44
"private": true,
55
"workspaces": [
6-
"packages/*"
6+
"packages/*",
7+
"playground/*"
78
],
89
"scripts": {
910
"build": "yarn workspaces foreach -vtp run build",
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { defineBuildConfig } from "unbuild"
2+
3+
export default defineBuildConfig({
4+
entries: [
5+
'./src/module',
6+
{ input: './src/runtime/', outDir: './dist/runtime/' },
7+
],
8+
declaration: true,
9+
rollup : {
10+
emitCJS : true,
11+
cjsBridge: true,
12+
esbuild : { tsconfig: 'tsconfig.build.json' }
13+
},
14+
externals: [
15+
'@nuxt/types',
16+
'@nuxt/schema',
17+
'@nuxt/schema-edge',
18+
]
19+
})
Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
11
{
22
"name": "@privyid/nugrpc-api-nuxt",
3+
"main": "./dist/module.cjs",
4+
"module": "./dist/module.mjs",
5+
"types": "./dist/module.d.ts",
6+
"exports": {
7+
".": {
8+
"require": "./dist/module.cjs",
9+
"import": "./dist/module.mjs",
10+
"types": "./dist/module.d.ts"
11+
}
12+
},
13+
"scripts": {
14+
"build": "unbuild",
15+
"test": "jest",
16+
"coverage": "jest --coverage"
17+
},
318
"packageManager": "[email protected]",
419
"devDependencies": {
5-
"@nuxt/types": "^2.15.8"
20+
"@nuxt/schema": "^0.1.0-edge",
21+
"@nuxt/types": "^2.15.8",
22+
"unbuild": "^0.7.0"
23+
},
24+
"dependencies": {
25+
"@privyid/nugrpc-api": "workspace:^",
26+
"defu": "^5.0.1",
27+
"pathe": "^0.2.0"
628
}
729
}
Lines changed: 93 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,102 @@
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"
36

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[];
1013
}
1114

12-
export default NugrpcApi
15+
function toHttps (url: string): string {
16+
return url.replace('http://', 'https://')
17+
}
1318

1419
// REQUIRED if publishing the module as npm package
15-
export const meta = {
20+
export const meta: ModuleMeta = {
1621
name : '@privyid/nugrpc-api',
1722
version : '1.0.0',
1823
configKey: 'nugrpcApi',
1924
}
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+
}
Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
import { Plugin } from '@nuxt/types'
2-
// import { setAxios } from '@privyid/nugrpc-api'
2+
import { setApi, createApi, ApiConfig } from '@privyid/nugrpc-api'
33

4-
const accessor: Plugin = (context) => {
5-
// setAxios($axios)
6-
context.req.headers
4+
const plugin: Plugin = (context, inject) => {
5+
const runtimeConfig = context.$config.nugrpcApi ?? {}
6+
const baseURL = process.client
7+
? (runtimeConfig.browserBaseURL || runtimeConfig.browserBaseUrl || runtimeConfig.baseURL || runtimeConfig.baseUrl || `<%= options.browserBaseURL || '' %>`)
8+
: (runtimeConfig.baseURL || runtimeConfig.baseUrl || process.env._AXIOS_BASE_URL_ || `<%= options.baseURL || '' %>`)
9+
10+
const options: ApiConfig = {
11+
baseURL,
12+
headers: {},
13+
}
14+
15+
if (process.server) {
16+
// Don't accept brotli encoding because Node can't parse it
17+
options.headers!['accept-encoding'] = 'gzip, deflate'
18+
}
19+
20+
const instance = createApi(options)
21+
22+
setApi(instance)
23+
inject('api', instance)
724
}
825

9-
export default accessor
26+
export default plugin

packages/nugrpc-api/build.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { defineBuildConfig } from "unbuild"
33
export default defineBuildConfig({
44
entries: [
55
'./src/index',
6-
'./src/error',
76
],
87
declaration: true,
98
rollup : {

packages/nugrpc-api/package.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@
99
"require": "./dist/index.cjs",
1010
"import": "./dist/index.mjs",
1111
"types": "./dist/index.d.ts"
12-
},
13-
"./error": {
14-
"require": "./dist/error.cjs",
15-
"import": "./dist/error.mjs",
16-
"types": "./dist/error.d.ts"
1712
}
1813
},
1914
"scripts": {
@@ -22,13 +17,14 @@
2217
"coverage": "jest --coverage"
2318
},
2419
"dependencies": {
25-
"abort-controller": "^3.0.0",
20+
"abortcontroller-polyfill": "^1.7.3",
2621
"axios": "^0.26.0",
2722
"defu": "^6.0.0",
2823
"native-abort-controller": "^1.0.4"
2924
},
3025
"devDependencies": {
3126
"@jest/globals": "28.1.0",
27+
"@types/url-join": "^4",
3228
"axios-mock-adapter": "^1.20.0",
3329
"cross-env": "^7.0.3",
3430
"jest": "28.1.0",

packages/nugrpc-api/src/dedupe.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import 'abortcontroller-polyfill/dist/abortcontroller-polyfill-only'
12
import Axios from "axios"
23
import MockAdapter from "axios-mock-adapter"
3-
import { useAxios } from "."
4-
import { AbortController } from "native-abort-controller"
4+
import { useApi } from "."
55

66
let mock: MockAdapter
77

@@ -17,7 +17,7 @@ describe('DedupeAdapter', () => {
1717
it('should cancel previous request with same requestId', async () => {
1818
mock.onGet('/api/ping').reply(200, { message: 'Pong' })
1919

20-
const api = useAxios()
20+
const api = useApi()
2121
const a = api.get('/api/ping', { requestId: 'ping' })
2222
const b = api.get('/api/ping', { requestId: 'ping' })
2323
const result = await Promise.allSettled([a, b])
@@ -29,7 +29,7 @@ describe('DedupeAdapter', () => {
2929
it('should do nothing if requestId is different', async () => {
3030
mock.onGet('/api/ping').reply(200, { message: 'Pong' })
3131

32-
const api = useAxios()
32+
const api = useApi()
3333
const a = api.get('/api/ping', { requestId: 'ping/a' })
3434
const b = api.get('/api/ping', { requestId: 'ping/b' })
3535
const result = await Promise.allSettled([a, b])
@@ -41,7 +41,7 @@ describe('DedupeAdapter', () => {
4141
it('should be able to cancel via AbortController', async () => {
4242
mock.onGet('/api/ping').reply(200, { message: 'Pong' })
4343

44-
const api = useAxios()
44+
const api = useApi()
4545
const controller = new AbortController()
4646
const signal = controller.signal
4747

@@ -61,7 +61,7 @@ describe('cancel', () => {
6161
it('should be cancel request only specific requestId', async () => {
6262
mock.onGet('/api/ping').reply(200, { message: 'Pong' })
6363

64-
const api = useAxios()
64+
const api = useApi()
6565
const a = api.get('/api/ping', { requestId: 'ping/i' })
6666
const b = api.get('/api/ping', { requestId: 'ping/j' })
6767

@@ -78,7 +78,7 @@ describe('cancelAll', () => {
7878
it('should be cancel all active request', async () => {
7979
mock.onGet('/api/ping').reply(200, { message: 'Pong' })
8080

81-
const api = useAxios()
81+
const api = useApi()
8282
const a = api.get('/api/ping', { requestId: 'ping/x' })
8383
const b = api.get('/api/ping', { requestId: 'ping/y' })
8484

packages/nugrpc-api/src/dedupe.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { AbortController } from "native-abort-controller"
21
import type { AxiosAdapter } from "axios"
32

43
export default class DedupeAdapter {

packages/nugrpc-api/src/error.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Axios from "axios"
22
import MockAdapter from "axios-mock-adapter"
3-
import { useAxios } from "."
3+
import { useApi } from "."
44
import { getCode, getMessage, isApiError } from "./error"
55

66
let mock: MockAdapter
@@ -22,7 +22,7 @@ describe('Error utils', () => {
2222
details: [],
2323
})
2424

25-
const api = useAxios()
25+
const api = useApi()
2626

2727
try {
2828
await api.get('/api/user')
@@ -49,7 +49,7 @@ describe('Error utils', () => {
4949
details: [],
5050
})
5151

52-
const api = useAxios()
52+
const api = useApi()
5353

5454
try {
5555
await api.get('/api/user')
@@ -66,7 +66,7 @@ describe('Error utils', () => {
6666
details: [],
6767
})
6868

69-
const api = useAxios()
69+
const api = useApi()
7070

7171
try {
7272
await api.get('/api/user')
@@ -99,7 +99,7 @@ describe('Error utils', () => {
9999
],
100100
})
101101

102-
const api = useAxios()
102+
const api = useApi()
103103
const normalError = new Error('Not Error')
104104
const resposeError = await (api.get('/api/not-found').catch((error) => error))
105105
const grpcError = await (api.get('/api/user').catch((error) => error))

0 commit comments

Comments
 (0)