-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmanifest-action-handler.ts
69 lines (56 loc) · 2.11 KB
/
manifest-action-handler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { createDebug } from "@/debug";
import { AppManifest, SaleorSchemaVersion } from "@/types";
import { parseSchemaVersion } from "@/util";
import {
ActionHandlerInterface,
ActionHandlerResult,
PlatformAdapterInterface,
} from "../shared/generic-adapter-use-case-types";
import { SaleorRequestProcessor } from "../shared/saleor-request-processor";
const debug = createDebug("create-manifest-handler");
export type CreateManifestHandlerOptions<T> = {
manifestFactory(context: {
appBaseUrl: string;
request: T;
/**
* Schema version is optional. During installation, Saleor will send it,
* so manifest can be generated according to the version. But it may
* be also requested from plain GET from the browser, so it may not be available
*/
schemaVersion?: SaleorSchemaVersion;
}): AppManifest | Promise<AppManifest>;
};
export class ManifestActionHandler<I> implements ActionHandlerInterface {
constructor(private adapter: PlatformAdapterInterface<I>) {}
private requestProcessor = new SaleorRequestProcessor(this.adapter);
async handleAction(options: CreateManifestHandlerOptions<I>): Promise<ActionHandlerResult> {
const { schemaVersion } = this.requestProcessor.getSaleorHeaders();
const parsedSchemaVersion = parseSchemaVersion(schemaVersion) ?? undefined;
const baseURL = this.adapter.getBaseUrl();
debug("Received request with schema version \"%s\" and base URL \"%s\"", schemaVersion, baseURL);
const invalidMethodResponse = this.requestProcessor.withMethod(["GET"]);
if (invalidMethodResponse) {
return invalidMethodResponse;
}
try {
const manifest = await options.manifestFactory({
appBaseUrl: baseURL,
request: this.adapter.request,
schemaVersion: parsedSchemaVersion,
});
debug("Executed manifest file");
return {
status: 200,
bodyType: "json",
body: manifest,
};
} catch (e) {
debug("Error while resolving manifest: %O", e);
return {
status: 500,
bodyType: "string",
body: "Error resolving manifest file.",
};
}
}
}