pl-api: support custom auth token
Signed-off-by: nicole mikołajczyk <git@mkljczk.pl>
This commit is contained in:
@ -292,6 +292,8 @@ interface PlApiClientConstructorOpts {
|
||||
onInstanceFetchSuccess?: (instance: Instance) => void;
|
||||
/** Executed when the initial instance fetch failed */
|
||||
onInstanceFetchError?: (error?: any) => void;
|
||||
/** Custom authorization token to use for requests */
|
||||
customAuthorizationToken?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -303,6 +305,7 @@ class PlApiClient {
|
||||
baseURL: string;
|
||||
#accessToken?: string;
|
||||
#iceshrimpAccessToken?: string;
|
||||
#customAuthorizationToken?: string;
|
||||
#instance: Instance = v.parse(instanceSchema, {});
|
||||
public request = request.bind(this) as typeof request;
|
||||
public features: Features = getFeatures(this.#instance);
|
||||
@ -329,6 +332,7 @@ class PlApiClient {
|
||||
fetchInstanceSignal,
|
||||
onInstanceFetchSuccess,
|
||||
onInstanceFetchError,
|
||||
customAuthorizationToken,
|
||||
}: PlApiClientConstructorOpts = {}) {
|
||||
this.baseURL = baseURL;
|
||||
this.#accessToken = accessToken;
|
||||
@ -6388,6 +6392,14 @@ class PlApiClient {
|
||||
return this.#iceshrimpAccessToken;
|
||||
}
|
||||
|
||||
get customAuthorizationToken(): string | undefined {
|
||||
return this.#customAuthorizationToken;
|
||||
}
|
||||
|
||||
set customAuthorizationToken(token: string | undefined) {
|
||||
this.#customAuthorizationToken = token;
|
||||
}
|
||||
|
||||
get instanceInformation() {
|
||||
return this.#instance;
|
||||
}
|
||||
|
||||
@ -82,7 +82,7 @@ interface RequestBody<Params = Record<string, any>> {
|
||||
|
||||
type RequestMeta = Pick<RequestBody, 'idempotencyKey' | 'onUploadProgress' | 'signal'>;
|
||||
|
||||
function request<T = any>(this: Pick<PlApiClient, 'accessToken' | 'iceshrimpAccessToken' | 'baseURL'>, input: URL | RequestInfo, {
|
||||
function request<T = any>(this: Pick<PlApiClient, 'accessToken' | 'customAuthorizationToken' | 'iceshrimpAccessToken' | 'baseURL'>, input: URL | RequestInfo, {
|
||||
body,
|
||||
method = body ? 'POST' : 'GET',
|
||||
params,
|
||||
@ -97,6 +97,7 @@ function request<T = any>(this: Pick<PlApiClient, 'accessToken' | 'iceshrimpAcce
|
||||
|
||||
if (input.startsWith('/api/iceshrimp/') && this.iceshrimpAccessToken) headers.set('Authorization', `Bearer ${this.iceshrimpAccessToken}`);
|
||||
else if (this.accessToken) headers.set('Authorization', `Bearer ${this.accessToken}`);
|
||||
else if (this.customAuthorizationToken) headers.set('Authorization', this.customAuthorizationToken);
|
||||
if (contentType !== '' && body) headers.set('Content-Type', contentType);
|
||||
if (idempotencyKey) headers.set('Idempotency-Key', contentType);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user