Skip to content

Commit 283561f

Browse files
authored
feat: add browser-safe fallback entry with no-op modules for frontend compatibility (#28)
This pull request adds a browser-safe fallback entry with no-op modules to enable frontend compatibility while maintaining the Node.js HTTP adapter's functionality. Key changes include excluding browser-specific files from documentation generation, adding browser-specific blueprint and decorator implementations, and updating the rollup bundling and package exports configurations.
1 parent cdf5846 commit 283561f

File tree

10 files changed

+113
-19
lines changed

10 files changed

+113
-19
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ The `@stone-js/node-http-adapter` brings your Stone.js application to life in th
172172

173173
This package is part of the Stone.js ecosystem, a modern JavaScript framework built around the Continuum Architecture.
174174

175-
Explore the full documentation: https://stonejs.dev
175+
Explore the full documentation: [https://stonejs.dev](https://stonejs.dev)
176176

177177
## API documentation
178178

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,14 @@
3030
"types": "./dist/index.d.ts",
3131
"exports": {
3232
".": {
33-
"types": "./dist/index.d.ts",
34-
"default": "./dist/index.js"
33+
"browser": {
34+
"types": "./dist/index.d.ts",
35+
"default": "./dist/browser.js"
36+
},
37+
"default": {
38+
"types": "./dist/index.d.ts",
39+
"default": "./dist/index.js"
40+
}
3541
}
3642
},
3743
"engines": {

rollup.config.mjs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import nodeExternals from 'rollup-plugin-node-externals'
88

99
export default [
1010
{
11-
input: 'src/**/*.ts',
11+
input: [
12+
'src/**/*.ts',
13+
'!src/browser/**/*'
14+
],
1215
output: [
1316
{ format: 'es', file: 'dist/index.js' }
1417
],
@@ -27,7 +30,33 @@ export default [
2730
]
2831
},
2932
{
30-
input: 'dist/**/*.d.ts',
33+
input: [
34+
'src/constants.ts',
35+
'src/errors/**/*.ts',
36+
'src/browser/**/*.ts'
37+
],
38+
output: [
39+
{ format: 'es', file: 'dist/browser.js' }
40+
],
41+
plugins: [
42+
multi(),
43+
nodeExternals(), // Must always be before `nodeResolve()`.
44+
nodeResolve({
45+
extensions: ['.js', '.mjs', '.ts'],
46+
exportConditions: ['node', 'import', 'require', 'default']
47+
}),
48+
typescript({
49+
noEmitOnError: true,
50+
tsconfig: './tsconfig.build.json',
51+
}),
52+
commonjs()
53+
]
54+
},
55+
{
56+
input: [
57+
'dist/**/*.d.ts',
58+
'!dist/browser/**/*.d.ts'
59+
],
3160
output: [{ format: 'es' , file: 'dist/index.d.ts' }],
3261
plugins: [
3362
multi(),

src/browser/decorators/NodeHttp.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { classDecoratorLegacyWrapper, ClassType } from '@stone-js/core'
2+
import { NodeHttpAdapterAdapterConfig } from '../../options/NodeHttpAdapterBlueprint'
3+
4+
/**
5+
* Interface for configuring the `NodeHttp` decorator.
6+
*
7+
* This interface extends `NodeHttpAdapterConfig` and allows partial customization
8+
* of the Node.js HTTP adapter blueprint configuration.
9+
*/
10+
export interface NodeHttpOptions extends Partial<NodeHttpAdapterAdapterConfig> {}
11+
12+
/**
13+
* A class decorator for registering a Node.js HTTP adapter in the Stone.js framework.
14+
*
15+
* The decorator modifies the `nodeHttpAdapterBlueprint` by merging the provided options
16+
* with the default configuration. It also registers the blueprint to the target class using
17+
* the `addBlueprint` utility.
18+
*
19+
* NB: This decorator is stubbed for browser environments compatibility and does not
20+
* perform any actual functionality in the browser. It is intended for use in Node.js environments
21+
* where the Node.js HTTP adapter is applicable.
22+
*
23+
* @template T - The type of the class being decorated, defaulting to `ClassType`.
24+
*
25+
* @param options - An object containing configuration options for the Node.js HTTP adapter.
26+
*
27+
* @returns A class decorator function.
28+
*
29+
* @example
30+
* ```typescript
31+
* import { NodeHttp } from '@stone-js/node-http';
32+
*
33+
* @NodeHttp({
34+
* url: 'http://localhost:3000',
35+
* default: true,
36+
* })
37+
* class MyHttpService {
38+
* // Service implementation
39+
* }
40+
* ```
41+
*/
42+
export const NodeHttp = <T extends ClassType = ClassType>(_options: NodeHttpOptions = {}): ClassDecorator => {
43+
return classDecoratorLegacyWrapper<T>((_target: T, _context: ClassDecoratorContext<T>): undefined => {})
44+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { NodeHttpAdapterBlueprint } from '../../options/NodeHttpAdapterBlueprint'
2+
3+
/**
4+
* Node HTTP adapter options.
5+
*
6+
* This object defines the configuration for the Node HTTP adapter.
7+
*
8+
* NB: This is a stub for browser environments and does not perform any actual functionality in the browser.
9+
*/
10+
export const nodeHttpAdapterBlueprint: NodeHttpAdapterBlueprint = { stone: { http: {}, adapters: [] } }

tests/NodeHttpAdapter.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ describe('NodeHttpAdapter', () => {
7474
}),
7575
get: vi.fn((key, fallback) => blueprint.values[key] ?? fallback),
7676
has: vi.fn((key) => key in blueprint.values),
77-
getAll: vi.fn(() => blueprint.values),
77+
getAll: vi.fn(() => blueprint.values)
7878
}
7979
blueprint.set('stone.logger.resolver', () => ({
8080
info: vi.fn(),

tests/decorators/NodeHttp.spec.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { Mock } from 'vitest'
2-
import { addBlueprint } from '@stone-js/core'
31
import { NodeHttp, NodeHttpOptions } from '../../src/decorators/NodeHttp'
2+
import { addBlueprint, classDecoratorLegacyWrapper } from '@stone-js/core'
3+
import { NodeHttp as BrowserNodeHttp } from '../../src/browser/decorators/NodeHttp'
44
import { nodeHttpAdapterBlueprint } from '../../src/options/NodeHttpAdapterBlueprint'
5+
import { nodeHttpAdapterBlueprint as browserNodeHttpAdapterBlueprint } from '../../src/browser/options/NodeHttpAdapterBlueprint'
56

67
/* eslint-disable @typescript-eslint/no-extraneous-class */
78

@@ -11,19 +12,22 @@ vi.mock('@stone-js/core', async (importOriginal) => {
1112
return {
1213
...actual,
1314
addBlueprint: vi.fn(() => {}),
14-
classDecoratorLegacyWrapper: (fn: Function) => {
15+
classDecoratorLegacyWrapper: vi.fn((fn: Function) => {
1516
fn()
1617
return fn
17-
}
18+
})
1819
}
1920
})
2021

2122
describe('NodeHttp', () => {
2223
it('should call addBlueprint with correct parameters', () => {
23-
(addBlueprint as Mock).mockReturnValueOnce(() => {})
24+
vi.mocked(addBlueprint).mockImplementation(() => {})
2425
const options: NodeHttpOptions = nodeHttpAdapterBlueprint.stone.adapters[0]
2526
NodeHttp(options)(class {})
27+
BrowserNodeHttp()(class {})
2628
expect(addBlueprint).toHaveBeenCalled()
29+
expect(classDecoratorLegacyWrapper).toHaveBeenCalledTimes(2)
30+
expect(addBlueprint).not.toHaveBeenCalledWith(expect.any(Function), expect.any(Object), browserNodeHttpAdapterBlueprint)
2731
})
2832

2933
it('should call addBlueprint with default options if none are provided', () => {

tests/resolvers.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const blueprint: any = {
88
}),
99
get: vi.fn((key, fallback) => blueprint.values[key] ?? fallback),
1010
has: vi.fn((key) => key in blueprint.values),
11-
getAll: vi.fn(() => blueprint.values),
11+
getAll: vi.fn(() => blueprint.values)
1212
}
1313

1414
describe('NodeHttpAdapter Resolvers', () => {

typedoc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"entryPoints": ["src/**/*.ts"], // Entry point for your project, change accordingly
3+
"exclude": ["src/browser/**/*"], // Exclude browser-specific files if any
34
"out": "docs", // Output directory for the generated documentation
45
"name": "Node HTTP Adapter", // Project name used in the docs
56
"includeVersion": false, // Optional: Include project version in the output

0 commit comments

Comments
 (0)