1
0
mirror of https://github.com/MisskeyIO/misskey synced 2024-12-25 20:18:18 +09:00
MisskeyIO/packages/backend/test/misc/mock-resolver.ts

37 lines
841 B
TypeScript
Raw Normal View History

2022-12-04 10:16:03 +09:00
import Resolver from '../../src/activitypub/resolver.js';
import { IObject } from '../../src/activitypub/type.js';
type MockResponse = {
type: string;
content: string;
};
export class MockResolver extends Resolver {
private _rs = new Map<string, MockResponse>();
public async _register(uri: string, content: string | Record<string, any>, type = 'application/activity+json') {
this._rs.set(uri, {
type,
2022-05-21 22:21:41 +09:00
content: typeof content === 'string' ? content : JSON.stringify(content),
});
}
@bindThis
public async resolve(value: string | IObject): Promise<IObject> {
if (typeof value !== 'string') return value;
const r = this._rs.get(value);
if (!r) {
throw {
2022-05-21 22:21:41 +09:00
name: 'StatusError',
statusCode: 404,
2022-05-21 22:21:41 +09:00
message: 'Not registed for mock',
};
}
const object = JSON.parse(r.content);
return object;
}
}