@beepbox.net/gofetch-client 0.2.4 → 0.2.6

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/dist/index.js CHANGED
@@ -151,6 +151,46 @@ function createFetcher(options = {}) {
151
151
  util: makeUtil(api)
152
152
  };
153
153
  }
154
+ // src/auth.ts
155
+ function currentHash() {
156
+ if (typeof window === "undefined")
157
+ return "";
158
+ return window.location.hash;
159
+ }
160
+ function buildLoginUrl(options) {
161
+ const base = (options.baseUrl ?? defaultBaseUrl).replace(/\/$/, "");
162
+ const url = new URL(`${base}/v0/auth/discord`);
163
+ url.searchParams.set("returnTo", options.returnTo);
164
+ return url.toString();
165
+ }
166
+ function parseOAuthCallback(hash = currentHash()) {
167
+ const raw = hash.startsWith("#") ? hash.slice(1) : hash;
168
+ if (!raw)
169
+ return null;
170
+ const params = new URLSearchParams(raw);
171
+ if (params.has("error")) {
172
+ return {
173
+ ok: false,
174
+ error: params.get("error") ?? "unknown",
175
+ errorDescription: params.get("error_description") ?? undefined
176
+ };
177
+ }
178
+ const accessToken = params.get("accessToken");
179
+ const userRaw = params.get("user");
180
+ if (!accessToken || !userRaw)
181
+ return null;
182
+ try {
183
+ const user = JSON.parse(userRaw);
184
+ return { ok: true, accessToken, user };
185
+ } catch {
186
+ return null;
187
+ }
188
+ }
189
+ function clearOAuthCallbackHash() {
190
+ if (typeof window === "undefined" || !window.location.hash)
191
+ return;
192
+ window.history.replaceState(null, "", window.location.pathname + window.location.search);
193
+ }
154
194
 
155
195
  // src/runtime.ts
156
196
  var defaultBaseUrl = "https://gofetch.blaqat.net";
@@ -161,7 +201,10 @@ function createClient(options = {}) {
161
201
  }
162
202
  export {
163
203
  treaty,
204
+ parseOAuthCallback,
164
205
  defaultBaseUrl,
165
206
  createFetcher,
166
- createClient
207
+ createClient,
208
+ clearOAuthCallbackHash,
209
+ buildLoginUrl
167
210
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@beepbox.net/gofetch-client",
3
- "version": "0.2.4",
3
+ "version": "0.2.6",
4
4
  "description": "Typesafe Eden treaty client for the gofetch API",
5
5
  "license": "UNLICENSED",
6
6
  "files": [