which-url 0.0.4 → 0.0.5
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/README.md +6 -6
- package/dist/index.d.ts +4 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +18 -13
- package/dist/resolve.d.ts +6 -2
- package/dist/resolve.d.ts.map +1 -1
- package/dist/types.d.ts +3 -3
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -167,15 +167,13 @@ APP_URL = "https://myapp.workers.dev"
|
|
|
167
167
|
|
|
168
168
|
Modern wrangler polyfills `process.env` from `[vars]`, so `which-url` picks it up automatically.
|
|
169
169
|
|
|
170
|
-
###
|
|
171
|
-
|
|
172
|
-
Re-resolves the URL from the current environment. Unlike the singleton (which catches errors), `createUrl()` throws in production if no URL is detected.
|
|
170
|
+
### Debugging
|
|
173
171
|
|
|
174
172
|
```typescript
|
|
175
|
-
import {
|
|
173
|
+
import { debug } from 'which-url'
|
|
176
174
|
|
|
177
|
-
|
|
178
|
-
url.
|
|
175
|
+
console.log(debug)
|
|
176
|
+
// "platform=vercel | source=provider | url=https://myapp.com | env=production"
|
|
179
177
|
```
|
|
180
178
|
|
|
181
179
|
## API
|
|
@@ -196,6 +194,8 @@ An object with URL properties and environment helpers.
|
|
|
196
194
|
| `port` | `string` | `""` or `"3000"` |
|
|
197
195
|
| `env` | `AppEnv` | `"production"` \| `"preview"` \| `"local"` |
|
|
198
196
|
| `platform` | `Platform` | `"vercel"` \| `"netlify"` \| ... \| `null` |
|
|
197
|
+
| `source` | `Source` | `"override"` \| `"provider"` \| `"browser"` \| `"fallback"` \| `null` |
|
|
198
|
+
| `debug` | `string` | `"platform=vercel \| source=provider \| url=https://myapp.com \| env=production"` |
|
|
199
199
|
| `isProduction` | `boolean` | |
|
|
200
200
|
| `isPreview` | `boolean` | |
|
|
201
201
|
| `isLocal` | `boolean` | |
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type { WhichUrl, AppEnv, Platform,
|
|
2
|
-
export declare function createUrl(options?: CreateUrlOptions): WhichUrl;
|
|
1
|
+
import type { WhichUrl, AppEnv, Platform, Source } from "./types";
|
|
3
2
|
declare let _resolved: WhichUrl;
|
|
4
3
|
export declare const href: string;
|
|
5
4
|
export declare const origin: string;
|
|
@@ -9,9 +8,11 @@ export declare const protocol: string;
|
|
|
9
8
|
export declare const port: string;
|
|
10
9
|
export declare const env: AppEnv;
|
|
11
10
|
export declare const platform: Platform;
|
|
11
|
+
export declare const source: Source;
|
|
12
|
+
export declare const debug: string;
|
|
12
13
|
export declare const isProduction: boolean;
|
|
13
14
|
export declare const isPreview: boolean;
|
|
14
15
|
export declare const isLocal: boolean;
|
|
15
16
|
export default _resolved;
|
|
16
|
-
export type { WhichUrl, AppEnv, Platform,
|
|
17
|
+
export type { WhichUrl, AppEnv, Platform, Source };
|
|
17
18
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AA0BjE,QAAA,IAAI,SAAS,EAAE,QAAQ,CAAA;AAyBvB,eAAO,MAAM,IAAI,EAAE,MAAuB,CAAA;AAC1C,eAAO,MAAM,MAAM,EAAE,MAAyB,CAAA;AAC9C,eAAO,MAAM,QAAQ,EAAE,MAA2B,CAAA;AAClD,eAAO,MAAM,IAAI,EAAE,MAAuB,CAAA;AAC1C,eAAO,MAAM,QAAQ,EAAE,MAA2B,CAAA;AAClD,eAAO,MAAM,IAAI,EAAE,MAAuB,CAAA;AAC1C,eAAO,MAAM,GAAG,EAAE,MAAsB,CAAA;AACxC,eAAO,MAAM,QAAQ,EAAE,QAA6B,CAAA;AACpD,eAAO,MAAM,MAAM,EAAE,MAAyB,CAAA;AAC9C,eAAO,MAAM,KAAK,EAAE,MAAwB,CAAA;AAC5C,eAAO,MAAM,YAAY,EAAE,OAAgC,CAAA;AAC3D,eAAO,MAAM,SAAS,EAAE,OAA6B,CAAA;AACrD,eAAO,MAAM,OAAO,EAAE,OAA2B,CAAA;AAGjD,eAAe,SAAS,CAAA;AAExB,YAAY,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -144,28 +144,25 @@ function normalizeUrl(raw) {
|
|
|
144
144
|
}
|
|
145
145
|
|
|
146
146
|
// src/resolve.ts
|
|
147
|
-
function resolveUrl(
|
|
147
|
+
function resolveUrl() {
|
|
148
148
|
const env = getEnv();
|
|
149
149
|
const override = getVar(env, "APP_URL");
|
|
150
150
|
if (override)
|
|
151
|
-
return normalizeUrl(override);
|
|
151
|
+
return { url: normalizeUrl(override), source: "override" };
|
|
152
152
|
for (const p of providers) {
|
|
153
153
|
if (p.detect(env)) {
|
|
154
154
|
const url = p.resolveUrl(env);
|
|
155
155
|
if (url)
|
|
156
|
-
return normalizeUrl(url);
|
|
156
|
+
return { url: normalizeUrl(url), source: "provider" };
|
|
157
157
|
}
|
|
158
158
|
}
|
|
159
159
|
if (typeof window !== "undefined" && window.location) {
|
|
160
|
-
return window.location.origin;
|
|
160
|
+
return { url: window.location.origin, source: "browser" };
|
|
161
161
|
}
|
|
162
162
|
const isProduction = env.NODE_ENV === "production";
|
|
163
163
|
if (!isProduction) {
|
|
164
164
|
const port = env.PORT || "3000";
|
|
165
|
-
return `http://localhost:${port}
|
|
166
|
-
}
|
|
167
|
-
if (options?.fallback) {
|
|
168
|
-
return normalizeUrl(options.fallback);
|
|
165
|
+
return { url: `http://localhost:${port}`, source: "fallback" };
|
|
169
166
|
}
|
|
170
167
|
throw new Error("which-url: Cannot detect app URL. Set APP_URL environment variable.");
|
|
171
168
|
}
|
|
@@ -200,11 +197,12 @@ function resolveEnv() {
|
|
|
200
197
|
}
|
|
201
198
|
|
|
202
199
|
// src/index.ts
|
|
203
|
-
function
|
|
204
|
-
const
|
|
205
|
-
const parsed = new URL(
|
|
200
|
+
function resolve() {
|
|
201
|
+
const { url, source } = resolveUrl();
|
|
202
|
+
const parsed = new URL(url);
|
|
206
203
|
const env = resolveEnv();
|
|
207
204
|
const platform = resolvePlatform();
|
|
205
|
+
const debug = `platform=${platform ?? "none"} | source=${source} | url=${parsed.origin} | env=${env}`;
|
|
208
206
|
return {
|
|
209
207
|
href: parsed.origin,
|
|
210
208
|
origin: parsed.origin,
|
|
@@ -214,6 +212,8 @@ function createUrl(options) {
|
|
|
214
212
|
port: parsed.port,
|
|
215
213
|
env,
|
|
216
214
|
platform,
|
|
215
|
+
source,
|
|
216
|
+
debug,
|
|
217
217
|
isProduction: env === "production",
|
|
218
218
|
isPreview: env === "preview",
|
|
219
219
|
isLocal: env === "local"
|
|
@@ -221,7 +221,7 @@ function createUrl(options) {
|
|
|
221
221
|
}
|
|
222
222
|
var _resolved;
|
|
223
223
|
try {
|
|
224
|
-
_resolved =
|
|
224
|
+
_resolved = resolve();
|
|
225
225
|
} catch (e) {
|
|
226
226
|
console.warn(`[which-url] Could not detect app URL. Set APP_URL (e.g. APP_URL=https://myapp.com or APP_URL=myapp.com)`);
|
|
227
227
|
_resolved = {
|
|
@@ -233,6 +233,8 @@ try {
|
|
|
233
233
|
port: "",
|
|
234
234
|
env: "local",
|
|
235
235
|
platform: null,
|
|
236
|
+
source: null,
|
|
237
|
+
debug: "platform=none | source=none | url=none | env=local",
|
|
236
238
|
isProduction: false,
|
|
237
239
|
isPreview: false,
|
|
238
240
|
isLocal: true
|
|
@@ -246,11 +248,14 @@ var protocol = _resolved.protocol;
|
|
|
246
248
|
var port = _resolved.port;
|
|
247
249
|
var env = _resolved.env;
|
|
248
250
|
var platform = _resolved.platform;
|
|
251
|
+
var source = _resolved.source;
|
|
252
|
+
var debug = _resolved.debug;
|
|
249
253
|
var isProduction = _resolved.isProduction;
|
|
250
254
|
var isPreview = _resolved.isPreview;
|
|
251
255
|
var isLocal = _resolved.isLocal;
|
|
252
256
|
var src_default = _resolved;
|
|
253
257
|
export {
|
|
258
|
+
source,
|
|
254
259
|
protocol,
|
|
255
260
|
port,
|
|
256
261
|
platform,
|
|
@@ -263,5 +268,5 @@ export {
|
|
|
263
268
|
host,
|
|
264
269
|
env,
|
|
265
270
|
src_default as default,
|
|
266
|
-
|
|
271
|
+
debug
|
|
267
272
|
};
|
package/dist/resolve.d.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export
|
|
1
|
+
import type { Platform, Source } from "./types";
|
|
2
|
+
export interface ResolveResult {
|
|
3
|
+
url: string;
|
|
4
|
+
source: Source;
|
|
5
|
+
}
|
|
6
|
+
export declare function resolveUrl(): ResolveResult;
|
|
3
7
|
export declare function resolvePlatform(): Platform;
|
|
4
8
|
//# sourceMappingURL=resolve.d.ts.map
|
package/dist/resolve.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolve.d.ts","sourceRoot":"","sources":["../src/resolve.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"resolve.d.ts","sourceRoot":"","sources":["../src/resolve.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AAE/C,MAAM,WAAW,aAAa;IAC5B,GAAG,EAAE,MAAM,CAAA;IACX,MAAM,EAAE,MAAM,CAAA;CACf;AAED,wBAAgB,UAAU,IAAI,aAAa,CA+B1C;AAED,wBAAgB,eAAe,IAAI,QAAQ,CAQ1C"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export type AppEnv = "production" | "preview" | "local";
|
|
2
2
|
export type Platform = "vercel" | "netlify" | "cloudflare" | "railway" | "fly" | "render" | "digitalocean" | "heroku" | null;
|
|
3
|
+
export type Source = "override" | "provider" | "browser" | "fallback" | null;
|
|
3
4
|
export interface WhichUrl {
|
|
4
5
|
href: string;
|
|
5
6
|
origin: string;
|
|
@@ -9,13 +10,12 @@ export interface WhichUrl {
|
|
|
9
10
|
port: string;
|
|
10
11
|
env: AppEnv;
|
|
11
12
|
platform: Platform;
|
|
13
|
+
source: Source;
|
|
14
|
+
debug: string;
|
|
12
15
|
isProduction: boolean;
|
|
13
16
|
isPreview: boolean;
|
|
14
17
|
isLocal: boolean;
|
|
15
18
|
}
|
|
16
|
-
export interface CreateUrlOptions {
|
|
17
|
-
fallback?: string;
|
|
18
|
-
}
|
|
19
19
|
export interface ProviderDetector {
|
|
20
20
|
name: string;
|
|
21
21
|
detect: (env: Record<string, string | undefined>) => boolean;
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,MAAM,GAAG,YAAY,GAAG,SAAS,GAAG,OAAO,CAAA;AAEvD,MAAM,MAAM,QAAQ,GAChB,QAAQ,GACR,SAAS,GACT,YAAY,GACZ,SAAS,GACT,KAAK,GACL,QAAQ,GACR,cAAc,GACd,QAAQ,GACR,IAAI,CAAA;AAER,MAAM,WAAW,QAAQ;IACvB,IAAI,EAAE,MAAM,CAAA;IACZ,MAAM,EAAE,MAAM,CAAA;IACd,QAAQ,EAAE,MAAM,CAAA;IAChB,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,EAAE,MAAM,CAAA;IAChB,IAAI,EAAE,MAAM,CAAA;IACZ,GAAG,EAAE,MAAM,CAAA;IACX,QAAQ,EAA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