Skip to content

Commit 0bb898a

Browse files
committed
fix(redirect): supports browser origin redirect when not in extension
1 parent e285ef5 commit 0bb898a

File tree

5 files changed

+26
-10
lines changed

5 files changed

+26
-10
lines changed

src/services/common/base-client.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ describe('base-client.ts', () => {
160160
delete: vi.spyOn(cacheStore, 'delete'),
161161
};
162162

163-
const client: TestableBaseClient = new TestableBaseClient({ cacheStore });
163+
const client: TestableBaseClient = new TestableBaseClient({ endpoint: 'http://my-endpoint', cacheStore });
164164
const fetch = vi.spyOn(CancellableFetch, 'fetch').mockResolvedValue(new Response());
165165

166166
const payload = {
@@ -335,7 +335,7 @@ describe('base-client.ts', () => {
335335

336336
const _cacheStore: CacheStore<Response> = new Map();
337337
_cacheStore.retention = 15;
338-
const _client = new TestableBaseClient({ cacheStore: _cacheStore });
338+
const _client = new TestableBaseClient({ endpoint: 'http://my-endpoint', cacheStore: _cacheStore });
339339

340340
await _client.endpointWithCache.cached();
341341
await _client.endpointWithCache.cached();

src/services/common/base-client.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export type BaseSettings<S extends RecursiveRecord = RecursiveRecord> = S & {
4343
corsPrefix?: string;
4444
};
4545

46-
export type BaseOptions<S extends RecursiveRecord = RecursiveRecord, R extends Response = Response> = S & {
46+
export type BaseOptions<S extends BaseSettings = BaseSettings, R extends Response = Response> = S & {
4747
/** Optional cache store to manage cache read/write */
4848
cacheStore?: CacheStore<R>;
4949
};

src/services/tvdb-client/clients/base-tvdb-client.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@ import type {
1313
import type { TvdbApi } from '~/services/tvdb-client/api/tvdb-api.endpoints';
1414

1515
import { TraktApiHeaders } from '~/models/trakt/trakt-client.model';
16-
import { BaseApiHeaders, type BaseBody, BaseClient, BaseHeaderContentType, parseBody, parseUrl } from '~/services/common/base-client';
16+
import {
17+
BaseApiHeaders,
18+
type BaseBody,
19+
BaseClient,
20+
BaseHeaderContentType,
21+
injectCorsProxyPrefix,
22+
parseBody,
23+
parseUrl,
24+
} from '~/services/common/base-client';
1725

1826
/** Needed to type Object assignment */
1927
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging -- To allow type extension
@@ -80,6 +88,7 @@ export class BaseTvdbClient extends BaseClient<TvdbApiQuery, TvdbApiResponse, Tv
8088
*/
8189
protected _parseUrl<T extends TvdbApiParam = TvdbApiParam>(template: TvdbApiTemplate<T>, params: T): URL {
8290
if (this.settings.version && !template.url.startsWith(`/${this.settings.version}`)) template.url = `/${this.settings.version}${template.url}`;
91+
injectCorsProxyPrefix(template, this.settings);
8392
return parseUrl<T>(template, params, this.settings.endpoint);
8493
}
8594

src/settings/traktv.api.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { TraktClientSettings } from '~/models/trakt/trakt-client.model';
22

3+
import { chromeRuntimeId } from '~/utils/browser/browser.utils';
4+
35
export const Config = {
46
UserAgent: `${import.meta.env.PKG_NAME}/${import.meta.env.PKG_VERSION}`,
57
};
@@ -20,12 +22,14 @@ export const Staging = {
2022

2123
const isProd = import.meta.env.PROD;
2224

23-
const client = isProd ? Production : Staging;
25+
const client = !isProd ? Production : Staging;
26+
27+
const browserRedirect = window.location.href.split('#').at(0) ?? client.RedirectionUrl;
2428

2529
export const traktClientSettings: TraktClientSettings = {
2630
client_id: client.ID,
2731
client_secret: client.Secret,
28-
redirect_uri: client.RedirectionUrl,
32+
redirect_uri: chromeRuntimeId ? client.RedirectionUrl : browserRedirect,
2933
endpoint: client.TraktEndpoint,
3034

3135
useragent: Config.UserAgent,

src/utils/browser/browser.utils.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
*
66
* @param options - The options for the new tab.
77
*/
8-
const openTab = (options: chrome.tabs.CreateProperties) => {
9-
console.info(options);
10-
window.open(options.url, options.active ? '_self' : '_blank');
11-
};
8+
const openTab = (options: chrome.tabs.CreateProperties) => window.open(options.url, options.active ? '_self' : '_blank');
129

1310
/**
1411
* @see [chrome.tabs.create](https://developer.chrome.com/docs/extensions/reference/tabs/#method-create)
1512
*/
1613
export const createTab = chrome?.tabs?.create ?? openTab;
14+
15+
/**
16+
* The ID of the current extension.
17+
* @see [chrome.runtime.id](https://developer.chrome.com/docs/extensions/reference/runtime/#property-id)
18+
*/
19+
export const chromeRuntimeId = chrome?.runtime?.id;

0 commit comments

Comments
 (0)