Skip to content

Commit a9e160a

Browse files
committed
refactor http methods and remove content-type restriction
1 parent 75ed6a8 commit a9e160a

File tree

8 files changed

+54
-25
lines changed

8 files changed

+54
-25
lines changed

packages/signals/signals-runtime/src/web/web-signals-types.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ interface BaseNetworkData {
7575
interface NetworkRequestData extends BaseNetworkData {
7676
action: 'request'
7777
url: string
78-
method: string
78+
method: HTTPMethod
7979
}
8080

8181
interface NetworkResponseData extends BaseNetworkData {
@@ -85,6 +85,15 @@ interface NetworkResponseData extends BaseNetworkData {
8585
ok: boolean
8686
}
8787

88+
export type HTTPMethod =
89+
| 'GET'
90+
| 'POST'
91+
| 'PUT'
92+
| 'DELETE'
93+
| 'PATCH'
94+
| 'HEAD'
95+
| 'OPTIONS'
96+
8897
export type NetworkData = NetworkRequestData | NetworkResponseData
8998

9099
export type NetworkSignal = RawSignal<'network', NetworkData>

packages/signals/signals/src/core/client/__tests__/client.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe(SignalsIngestClient, () => {
4949
data: {
5050
hello: 'how are you',
5151
},
52-
method: 'post',
52+
method: 'POST',
5353
url: 'http://foo.com',
5454
},
5555
})
@@ -63,7 +63,7 @@ describe(SignalsIngestClient, () => {
6363
"data": {
6464
"hello": "XXX",
6565
},
66-
"method": "post",
66+
"method": "POST",
6767
"url": "http://foo.com",
6868
}
6969
`)

packages/signals/signals/src/core/client/__tests__/redact.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ describe(redactSignalData, () => {
9393
{
9494
contentType: 'application/json',
9595
action: 'request',
96-
method: 'post',
96+
method: 'POST',
9797
url: 'http://foo.com',
9898
data: { name: 'John Doe', age: 30 },
9999
},
@@ -103,7 +103,7 @@ describe(redactSignalData, () => {
103103
{
104104
contentType: 'application/json',
105105
action: 'request',
106-
method: 'post',
106+
method: 'POST',
107107
url: 'http://foo.com',
108108
data: { name: 'XXX', age: 999 },
109109
},

packages/signals/signals/src/core/signal-generators/network-gen/__tests__/network-generator.test.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,22 +126,40 @@ describe(NetworkGenerator, () => {
126126
unregister()
127127
})
128128

129-
it('should emit response signal but not request signal if content-type for request is not recognized ', async () => {
129+
it('should emit response signal and request signal, regardless of content type', async () => {
130130
const mockEmitter = { emit: jest.fn() }
131131
const networkGenerator = new TestNetworkGenerator()
132132
const unregister = networkGenerator.register(
133133
mockEmitter as unknown as SignalEmitter
134134
)
135135

136136
await window.fetch(`/api`, {
137-
method: 'GET',
137+
method: 'POST',
138138
headers: { 'content-type': 'text/html' },
139139
body: 'hello world',
140140
})
141141

142142
await sleep(100)
143143
expect(mockEmitter.emit.mock.calls).toMatchInlineSnapshot(`
144144
[
145+
[
146+
{
147+
"data": {
148+
"action": "request",
149+
"contentType": "text/html",
150+
"data": "hello world",
151+
"method": "POST",
152+
"url": "http://localhost/api",
153+
},
154+
"metadata": {
155+
"filters": {
156+
"allowed": [],
157+
"disallowed": [],
158+
},
159+
},
160+
"type": "network",
161+
},
162+
],
145163
[
146164
{
147165
"data": {

packages/signals/signals/src/core/signal-generators/network-gen/index.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ import {
1212
NetworkRequestHandler,
1313
NetworkResponseHandler,
1414
} from './network-interceptor'
15-
import {
16-
containsJSONContentType,
17-
containsJSONParseableContentType,
18-
tryJSONParse,
19-
} from './helpers'
15+
import { containsJSONContentType, tryJSONParse } from './helpers'
2016

2117
export type NetworkSettingsConfigSettings = Pick<
2218
SignalsSettingsConfig,
@@ -58,10 +54,6 @@ export class NetworkGenerator implements SignalGenerator {
5854
})
5955

6056
const handleRequest: NetworkRequestHandler = (rq) => {
61-
if (!containsJSONParseableContentType(rq.headers)) {
62-
return
63-
}
64-
6557
if (!rq.url || !this.filter.isAllowed(rq.url)) {
6658
return
6759
}

packages/signals/signals/src/core/signal-generators/network-gen/network-interceptor.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,22 @@ import {
1010
let origFetch: typeof window.fetch
1111
let origXMLHttpRequest: typeof XMLHttpRequest
1212

13+
export type HTTPMethod =
14+
| 'GET'
15+
| 'POST'
16+
| 'PUT'
17+
| 'DELETE'
18+
| 'PATCH'
19+
| 'HEAD'
20+
| 'OPTIONS'
21+
22+
const toHTTPMethod = (method: string): HTTPMethod => {
23+
return method.toUpperCase() as HTTPMethod
24+
}
25+
1326
export interface NetworkInterceptorRequest {
1427
url: string
15-
method: string
28+
method: HTTPMethod
1629
contentType: string | undefined
1730
body: string | undefined
1831
headers: Headers | undefined
@@ -42,7 +55,7 @@ const createInterceptorRequest = ({
4255
}: {
4356
url: URL | string
4457
body: string | undefined
45-
method: string
58+
method: HTTPMethod
4659
headers: Headers | undefined
4760
id: string
4861
}): NetworkInterceptorRequest => ({
@@ -130,7 +143,7 @@ export class NetworkInterceptor {
130143
createInterceptorRequest({
131144
url: normalizedURL,
132145
body: typeof options?.body == 'string' ? options.body : undefined,
133-
method: options?.method ?? 'GET',
146+
method: options?.method ? toHTTPMethod(options.method) : 'GET',
134147
headers,
135148
id,
136149
})
@@ -185,7 +198,7 @@ export class NetworkInterceptor {
185198
const OrigXMLHttpRequest = window.XMLHttpRequest
186199
class InterceptedXMLHttpRequest extends OrigXMLHttpRequest {
187200
_reqURL?: string
188-
_reqMethod?: string
201+
_reqMethod?: HTTPMethod
189202
_reqBody?: XMLHttpRequestBodyInit
190203
_reqHeaders?: Headers
191204
_reqId = createRequestId()
@@ -263,7 +276,7 @@ export class NetworkInterceptor {
263276
const [method, url] = args
264277
try {
265278
this._reqURL = url.toString()
266-
this._reqMethod = method
279+
this._reqMethod = toHTTPMethod(method)
267280
} catch (err) {
268281
console.log('Error handling request (open)', err)
269282
}

packages/signals/signals/src/types/__tests__/create-network-signal.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe(createNetworkSignal, () => {
2121
const data: NetworkData = {
2222
action: 'request',
2323
url: 'http://example.com',
24-
method: 'post',
24+
method: 'POST',
2525
data: { key: 'value' },
2626
contentType: 'application/json',
2727
}

packages/signals/signals/src/types/factories.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ export const createNetworkSignal = (
6565
data: {
6666
...data,
6767
url: normalizeUrl(data.url),
68-
...(data.action === 'request'
69-
? { method: data.method.toUpperCase() }
70-
: {}),
7168
},
7269
metadata: metadata,
7370
}

0 commit comments

Comments
 (0)