Skip to content

Commit af9efbb

Browse files
committed
fix: import node-fetch correctly.
1 parent 73d2135 commit af9efbb

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

src/view/view.module.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ViewCategoryController } from './view.category.controller';
66
import { ViewSitemapController } from './view.sitemap.controller';
77
import { ViewSSRCacheService } from './view.ssr.cache.service';
88
import { ViewSSRController } from './view.ssr.controller';
9+
import { ViewSSRFetchService } from './view.ssr.fetch.service';
910

1011
const cache = new ViewSSRCacheService();
1112

@@ -20,6 +21,15 @@ const cache = new ViewSSRCacheService();
2021
provide: ViewSSRCacheService,
2122
useValue: cache,
2223
},
24+
{
25+
provide: ViewSSRFetchService,
26+
useFactory: () =>
27+
new ViewSSRFetchService((...args) =>
28+
import('node-fetch').then((module) =>
29+
module.default(...args)
30+
)
31+
),
32+
},
2333
],
2434
exports: [ViewSSRCacheService],
2535
controllers: [

src/view/view.ssr.controller.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import {
1010
import * as fs from 'fs';
1111
import { JSDOM } from 'jsdom';
1212
import * as path from 'path';
13-
import fetch from 'node-fetch';
1413

1514
import { AdminGuard } from '../admin/admin.guard';
1615

1716
import { ViewSSRCacheService } from './view.ssr.cache.service';
1817

1918
import { OptionalPipe } from '../helper/OptionalPipe';
2019
import { StringPipe } from '../helper/StringPipe';
20+
import { ViewSSRFetchService } from './view.ssr.fetch.service';
2121

2222
@Controller()
2323
export class ViewSSRController {
@@ -29,7 +29,10 @@ export class ViewSSRController {
2929
private readonly attachments: string[];
3030
private readonly timeout: number;
3131

32-
constructor(private cache: ViewSSRCacheService) {
32+
constructor(
33+
private cache: ViewSSRCacheService,
34+
private fetch: ViewSSRFetchService
35+
) {
3336
const frontendDir = process.env.SSR_FRONTEND_DIR || process.cwd();
3437
this.frontendEvent = process.env.SSR_FRONTEND_EVENT || 'app-loaded';
3538
this.indexHTML = fs.readFileSync(
@@ -87,7 +90,8 @@ export class ViewSSRController {
8790
// Poly-fill some window functions here.
8891
// eslint-disable-next-line @typescript-eslint/no-empty-function
8992
dom.window.scrollTo = () => {};
90-
dom.window.fetch = fetch as unknown as typeof dom.window.fetch;
93+
dom.window.fetch = this.fetch
94+
.fetch as unknown as typeof dom.window.fetch;
9195

9296
const timeout = setTimeout(() => {
9397
this.logger.warn(

src/view/view.ssr.fetch.service.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Injectable } from '@nestjs/common';
2+
import type { RequestInfo, RequestInit, Response } from 'node-fetch';
3+
4+
export type FetchType = (
5+
url: RequestInfo,
6+
init?: RequestInit
7+
) => Promise<Response>;
8+
9+
@Injectable()
10+
export class ViewSSRFetchService {
11+
constructor(private fetchImpl: FetchType) {}
12+
13+
get fetch(): FetchType {
14+
return this.fetchImpl;
15+
}
16+
}

0 commit comments

Comments
 (0)