Revert last canary flag PR and use a better method (#5535)
* Revert "Implement Labrinth Canary API flag (#5531)"
This reverts commit 3b21944a75.
* Use Labrinth-Canary header instead of cookie for API canary
* prepr
* fix rebase
This commit is contained in:
@@ -1,37 +0,0 @@
|
||||
import { AbstractFeature, type FeatureConfig } from '../core/abstract-feature'
|
||||
|
||||
export const LABRINTH_CANARY_COOKIE = 'labrinth-canary=always'
|
||||
|
||||
export interface CanaryCookieConfig extends FeatureConfig {
|
||||
getCookie?: () => string | undefined | Promise<string | undefined>
|
||||
}
|
||||
|
||||
export class CanaryCookieFeature extends AbstractFeature {
|
||||
declare protected config: CanaryCookieConfig
|
||||
|
||||
constructor(config?: CanaryCookieConfig) {
|
||||
super(config)
|
||||
}
|
||||
|
||||
shouldApply(context: Parameters<AbstractFeature['shouldApply']>[0]): boolean {
|
||||
return super.shouldApply(context) && context.options.api === 'labrinth'
|
||||
}
|
||||
|
||||
async execute<T>(next: () => Promise<T>, context: Parameters<AbstractFeature['execute']>[1]) {
|
||||
const cookie = this.config.getCookie ? await this.config.getCookie() : LABRINTH_CANARY_COOKIE
|
||||
if (!cookie) {
|
||||
return next()
|
||||
}
|
||||
|
||||
const headers = { ...(context.options.headers ?? {}) }
|
||||
const existingCookie = headers.cookie ?? headers.Cookie
|
||||
|
||||
if (!existingCookie?.split('; ').includes(cookie)) {
|
||||
headers.cookie = existingCookie ? `${existingCookie}; ${cookie}` : cookie
|
||||
delete headers.Cookie
|
||||
context.options.headers = headers
|
||||
}
|
||||
|
||||
return next()
|
||||
}
|
||||
}
|
||||
@@ -9,11 +9,6 @@ export {
|
||||
} from './core/abstract-websocket'
|
||||
export { ModrinthApiError, ModrinthServerError } from './core/errors'
|
||||
export { type AuthConfig, AuthFeature } from './features/auth'
|
||||
export {
|
||||
type CanaryCookieConfig,
|
||||
CanaryCookieFeature,
|
||||
LABRINTH_CANARY_COOKIE,
|
||||
} from './features/canary-cookie'
|
||||
export {
|
||||
type CircuitBreakerConfig,
|
||||
CircuitBreakerFeature,
|
||||
|
||||
Reference in New Issue
Block a user