@superdurable/connector-apispace 0.1.0 → 0.1.1
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.
- package/dex-connector.json +1 -1
- package/dist/index.js +40 -14
- package/package.json +1 -1
package/dex-connector.json
CHANGED
package/dist/index.js
CHANGED
|
@@ -107,35 +107,61 @@ var GetCurrentWeatherOperation = class {
|
|
|
107
107
|
}
|
|
108
108
|
const early = classifyEarlyStatus(response.status, response.headers);
|
|
109
109
|
if (early) return early;
|
|
110
|
+
if (response.body.length > this.client.maxResponseBytes) return branch("invalidResponse", "RESPONSE_TOO_LARGE", "APISpace response exceeded the configured limit");
|
|
110
111
|
if (response.status !== 200) {
|
|
111
112
|
const kind = response.status === 401 ? "AUTHENTICATION" : response.status === 403 || response.status === 416 ? "AUTHORIZATION" : response.status === 400 || response.status === 404 ? "VALIDATION" : "PROVIDER_REJECTION";
|
|
112
113
|
return branch("providerRejected", kind, `APISpace rejected weather query with HTTP ${response.status}`);
|
|
113
114
|
}
|
|
114
|
-
let
|
|
115
|
+
let wire;
|
|
115
116
|
try {
|
|
116
|
-
|
|
117
|
+
wire = fieldsByName(JSON.parse(new TextDecoder().decode(response.body), readProviderNumber));
|
|
117
118
|
} catch {
|
|
118
|
-
return
|
|
119
|
+
return invalidWeather();
|
|
119
120
|
}
|
|
120
|
-
if (!
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
121
|
+
if (!wire || typeof wire.status !== "bigint") return invalidWeather();
|
|
122
|
+
const result = wire.result == null ? {} : fieldsByName(wire.result);
|
|
123
|
+
if (!result) return invalidWeather();
|
|
124
|
+
const location = result.location == null ? {} : fieldsByName(result.location);
|
|
125
|
+
const observation = result.realtime == null ? {} : fieldsByName(result.realtime);
|
|
126
|
+
if (!location || !observation) return invalidWeather();
|
|
127
|
+
const { name: city } = location;
|
|
128
|
+
const { text: condition, temp: temperature, rh: humidity } = observation;
|
|
129
|
+
const updated = result.last_update;
|
|
130
|
+
if ([city, condition, updated].some((value) => value != null && typeof value !== "string") || temperature != null && (typeof temperature !== "number" || !Number.isFinite(temperature)) || humidity != null && typeof humidity !== "bigint") return invalidWeather();
|
|
131
|
+
if (wire.status !== 0n) {
|
|
132
|
+
return branch("providerRejected", "PROVIDER_REJECTION", `APISpace rejected weather query with status ${wire.status}`);
|
|
133
|
+
}
|
|
134
|
+
if (typeof city !== "string" || !city || typeof condition !== "string" || !condition || typeof temperature !== "number" || typeof humidity !== "bigint" || humidity < 0n || humidity > 100n || typeof updated !== "string" || !updated) return invalidWeather();
|
|
135
|
+
return newQueryBranch("found", {
|
|
136
|
+
city,
|
|
137
|
+
condition,
|
|
138
|
+
temperatureCelsius: temperature,
|
|
139
|
+
humidityPercent: Number(humidity),
|
|
140
|
+
updatedAtBeijing: updated
|
|
141
|
+
});
|
|
128
142
|
}
|
|
129
143
|
};
|
|
130
144
|
function classifyEarlyStatus(status, headers) {
|
|
131
|
-
const retryAfterHeader = headers["retry-after"];
|
|
145
|
+
const retryAfterHeader = Object.entries(headers).find(([name]) => name.toLowerCase() === "retry-after")?.[1];
|
|
132
146
|
const retryAfterMs = parseRetryAfter(Array.isArray(retryAfterHeader) ? retryAfterHeader[0] ?? "" : retryAfterHeader ?? "");
|
|
133
147
|
if (status === 429 || status === 413) return retry("RATE_LIMIT", "APISpace rate limited weather requests", retryAfterMs);
|
|
134
148
|
if (status === 408 || status >= 500) return retry("AVAILABILITY", "APISpace weather is temporarily unavailable", retryAfterMs);
|
|
135
149
|
return void 0;
|
|
136
150
|
}
|
|
137
|
-
function
|
|
138
|
-
return
|
|
151
|
+
function invalidWeather() {
|
|
152
|
+
return branch("invalidResponse", "PROTOCOL", "APISpace returned an invalid weather response");
|
|
153
|
+
}
|
|
154
|
+
function fieldsByName(value) {
|
|
155
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) return void 0;
|
|
156
|
+
return Object.fromEntries(Object.entries(value).map(([name, field]) => [name.toLowerCase(), field]));
|
|
157
|
+
}
|
|
158
|
+
function readProviderNumber(key, value, context) {
|
|
159
|
+
if (!["status", "rh"].includes(key.toLowerCase()) || typeof value !== "number") return value;
|
|
160
|
+
const source = context?.source;
|
|
161
|
+
if (source === void 0) return Number.isSafeInteger(value) ? BigInt(value) : false;
|
|
162
|
+
if (!/^-?\d+$/.test(source) || source.length > 20) return false;
|
|
163
|
+
const integer = BigInt(source);
|
|
164
|
+
return integer >= -(1n << 63n) && integer < 1n << 63n ? integer : false;
|
|
139
165
|
}
|
|
140
166
|
function isValidAPIToken(value) {
|
|
141
167
|
return value.length > 0 && value.length <= 2048 && /^[!-~]+$/.test(value);
|