|
| 1 | +import { join } from 'node:path' |
1 | 2 | import type { DevupApiOptions } from '@devup-api/core' |
| 3 | +import { createUrlMap, generateInterface } from '@devup-api/generator' |
| 4 | +import { createTmpDir, readOpenapi, writeInterface } from '@devup-api/utils' |
| 5 | +import { devupApiWebpackPlugin } from '@devup-api/webpack-plugin' |
2 | 6 | import type { NextConfig } from 'next' |
| 7 | +export function DevupUI( |
| 8 | + config: NextConfig, |
| 9 | + options: DevupApiOptions = {}, |
| 10 | +): NextConfig { |
| 11 | + const isTurbo = |
| 12 | + process.env.TURBOPACK === '1' || process.env.TURBOPACK === 'auto' |
| 13 | + if (isTurbo) { |
| 14 | + const tempDir = createTmpDir(options?.tempDir) |
| 15 | + const schema = readOpenapi(options?.openapiFile) |
3 | 16 |
|
4 | | -export default function devupApiPlugin(_options?: DevupApiOptions) { |
5 | | - return (nextConfig: NextConfig): NextConfig => { |
6 | | - return { |
7 | | - ...nextConfig, |
8 | | - // Extend Next.js configuration |
9 | | - } |
| 17 | + writeInterface( |
| 18 | + join(tempDir, 'api.d.ts'), |
| 19 | + generateInterface(schema, options), |
| 20 | + ) |
| 21 | + // Create urlMap and set environment variable |
| 22 | + const urlMap = createUrlMap(schema, options) |
| 23 | + config.env ??= {} |
| 24 | + Object.assign(config.env, { |
| 25 | + DEVUP_API_URL_MAP: JSON.stringify(urlMap), |
| 26 | + }) |
| 27 | + return config |
10 | 28 | } |
| 29 | + |
| 30 | + const webpack = config.webpack |
| 31 | + config.webpack = (config, _options) => { |
| 32 | + config.plugins.push(new devupApiWebpackPlugin(options)) |
| 33 | + if (typeof webpack === 'function') return webpack(config, _options) |
| 34 | + return config |
| 35 | + } |
| 36 | + return config |
11 | 37 | } |
0 commit comments