@t0.labs/daxta 1.1.7 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -70,7 +70,7 @@ function pickHeaders(request) {
70
70
  const name = rawName.toLowerCase();
71
71
  if (SKIP_HEADERS.has(name))
72
72
  continue;
73
- if (name !== 'content-type' && !name.startsWith('x-'))
73
+ if (name !== 'content-type' && name !== 'authorization' && !name.startsWith('x-'))
74
74
  continue;
75
75
  if (rawValue == null || rawValue === '')
76
76
  continue;
@@ -78,27 +78,102 @@ function pickHeaders(request) {
78
78
  }
79
79
  return Object.keys(headers).length ? headers : undefined;
80
80
  }
81
- function recordFromSuperagent(request, res) {
82
- const rawUrl = request.url || '';
83
- const url = new URL(rawUrl, 'http://localhost');
81
+ function pickFetchHeaders(initHeaders) {
82
+ if (!initHeaders)
83
+ return undefined;
84
+ const headerObj = {};
85
+ if (typeof initHeaders.forEach === 'function') {
86
+ initHeaders.forEach((val, key) => {
87
+ headerObj[key] = val;
88
+ });
89
+ }
90
+ else if (Array.isArray(initHeaders)) {
91
+ for (const pair of initHeaders) {
92
+ if (Array.isArray(pair) && pair.length >= 2)
93
+ headerObj[String(pair[0])] = pair[1];
94
+ }
95
+ }
96
+ else if (typeof initHeaders === 'object') {
97
+ Object.assign(headerObj, initHeaders);
98
+ }
99
+ return pickHeaders({ header: headerObj });
100
+ }
101
+ function tryParseJson(raw) {
102
+ if (!raw)
103
+ return undefined;
104
+ try {
105
+ return JSON.parse(raw);
106
+ }
107
+ catch {
108
+ return raw;
109
+ }
110
+ }
111
+ function recordHttpHit(params) {
112
+ const url = new URL(params.rawUrl, 'http://localhost');
84
113
  const test = currentTestName();
85
- const pathTemplate = (0, path_util_1.templatize)(url.pathname, test, (request.method || 'GET').toLowerCase());
114
+ const method = (params.method || 'GET').toLowerCase();
115
+ const pathTemplate = (0, path_util_1.templatize)(url.pathname, test, method);
86
116
  if (!pathTemplate)
87
117
  return;
88
118
  const query = Object.fromEntries(url.searchParams.entries());
89
119
  const pathParams = (0, path_util_1.extractPathParams)(pathTemplate, url.pathname);
90
120
  hits.push({
91
- method: (request.method || 'GET').toLowerCase(),
121
+ method,
92
122
  path: pathTemplate,
123
+ status: params.status ?? 0,
124
+ reqBody: params.reqBody,
125
+ resBody: params.resBody,
126
+ query: Object.keys(query).length ? query : undefined,
127
+ pathParams: Object.keys(pathParams).length ? pathParams : undefined,
128
+ headers: params.headers,
129
+ test,
130
+ });
131
+ }
132
+ function recordFromSuperagent(request, res) {
133
+ recordHttpHit({
134
+ method: request.method || 'GET',
135
+ rawUrl: request.url || '',
93
136
  status: res.status ?? 0,
94
137
  reqBody: request._data,
95
138
  resBody: res.body,
96
- query: Object.keys(query).length ? query : undefined,
97
- pathParams: Object.keys(pathParams).length ? pathParams : undefined,
98
139
  headers: pickHeaders(request),
99
- test,
100
140
  });
101
141
  }
142
+ function patchFetch() {
143
+ if (g.__DAXTA_FETCH_PATCHED__ || typeof globalThis.fetch !== 'function')
144
+ return;
145
+ g.__DAXTA_FETCH_PATCHED__ = true;
146
+ const originalFetch = globalThis.fetch;
147
+ globalThis.fetch = async function daxtaFetch(input, init) {
148
+ const response = await originalFetch.call(this, input, init);
149
+ try {
150
+ const rawUrl = typeof input === 'string' ? input : input instanceof URL ? input.toString() : input.url;
151
+ const method = init?.method || (typeof input === 'object' && input !== null && 'method' in input ? input.method : 'GET');
152
+ const reqBody = typeof init?.body === 'string' ? tryParseJson(init.body) : undefined;
153
+ const headers = pickFetchHeaders(init?.headers ?? (typeof input === 'object' && input !== null && 'headers' in input ? input.headers : undefined));
154
+ let resBody;
155
+ try {
156
+ const text = await response.clone().text();
157
+ resBody = text ? tryParseJson(text) : undefined;
158
+ }
159
+ catch {
160
+ // ignore unread/uncloneable body
161
+ }
162
+ recordHttpHit({
163
+ method,
164
+ rawUrl,
165
+ status: response.status,
166
+ reqBody,
167
+ resBody,
168
+ headers,
169
+ });
170
+ }
171
+ catch {
172
+ // recording must never fail a test
173
+ }
174
+ return response;
175
+ };
176
+ }
102
177
  function patchSuperagent(superagent) {
103
178
  const proto = superagent.Request?.prototype;
104
179
  if (!proto?.end || proto.__daxtaPatched)
@@ -143,6 +218,7 @@ function install() {
143
218
  // ignore
144
219
  }
145
220
  }
221
+ patchFetch();
146
222
  if (!g.__DAXTA_FLUSH_HOOK__) {
147
223
  g.__DAXTA_FLUSH_HOOK__ = true;
148
224
  const flushSafe = () => {
package/dist/setup.js CHANGED
@@ -1,7 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ const incremental_1 = require("./build/incremental");
4
+ const config_1 = require("./config");
3
5
  const recorder_1 = require("./recorder");
4
6
  (0, recorder_1.install)();
5
7
  afterAll(() => {
8
+ if ((0, config_1.getConfig)().writeMode === 'incremental') {
9
+ (0, incremental_1.flushAndRebuild)({ silent: true, html: true });
10
+ return;
11
+ }
6
12
  (0, recorder_1.flush)();
7
13
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@t0.labs/daxta",
3
- "version": "1.1.7",
3
+ "version": "1.2.0",
4
4
  "description": "DAxTA — Tested behavior. Trusted API docs. Generate API documentation from observed test execution.",
5
5
  "license": "MIT",
6
6
  "author": {