Skip to content

Commit

Permalink
Implements module publishing (create only)
Browse files Browse the repository at this point in the history
  • Loading branch information
liamgriffiths committed Aug 7, 2024
1 parent e39fdcf commit 5863aba
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 9 deletions.
17 changes: 16 additions & 1 deletion src/Module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
import { Node, Options } from "substrate/Node";
import { FutureInput } from "substrate/Future";

type ModuleId = `mod_${string}`;

export type SerializableModule = {
nodes: Node[];
inputs: ModuleInputs;
};

export type NewModule = SerializableModule & { name: string };
export type UpdateModule = NewModule & { id: ModuleId };
export type PublishableModule = NewModule; // | UpdateModule; (TODO: implement update module)
export type PublishedModule = {
id: ModuleId;
uri: string;
};

export type ModuleInputs = Record<string, FutureInput>;

type ModuleIn =
Expand All @@ -9,7 +24,7 @@ type ModuleIn =
inputs: Record<string, any>;
}
| {
module_id: any;
module_id: ModuleId;
inputs: Record<string, any>;
}
| {
Expand Down
48 changes: 40 additions & 8 deletions src/Substrate.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { SubstrateError, RequestTimeoutError } from "substrate/Error";
import { VERSION } from "substrate/version";
import OpenAPIjson from "substrate/openapi.json";
import { SubstrateResponse } from "substrate/SubstrateResponse";
import {
asSubstratePublishedModuleResponse,
SubstrateResponse,
} from "substrate/SubstrateResponse";
import { SubstrateStreamingResponse } from "substrate/SubstrateStreamingResponse";
import { Node } from "substrate/Node";
import { Future } from "substrate/Future";
import { getPlatformProperties } from "substrate/Platform";
import { deflate } from "pako";
import { randomString } from "substrate/idGenerator";
import { ModuleInputs } from "./Module";
import { SerializableModule, PublishableModule } from "substrate/Module";

type Configuration = {
/**
Expand Down Expand Up @@ -298,7 +301,7 @@ export class Substrate {
* Returns an object that represents a publishable "module" or code that can be used to construct
* a `Module` node.
*/
serialize: ({ nodes, inputs }: { nodes: Node[]; inputs: ModuleInputs }) => {
serialize: ({ nodes, inputs }: SerializableModule) => {
const inputIdToName = {};
const inputNameToSchema = {};

Expand Down Expand Up @@ -330,12 +333,41 @@ export class Substrate {

/**
* Publishes a module on substrate.run
* A successful response will contain the module id and web uri
*/
publish: async (_publishable: any) => {
console.log("not implemented yet");
let publication;
return publication;
publish: async (
publishable: PublishableModule,
// endpoint: string = "https://substrate.run/api/modules",
endpoint: string = "http://localhost:3000/api/modules",
) => {
const serialized = this.module.serialize({
nodes: publishable.nodes,
inputs: publishable.inputs,
});

const body = {
module: { name: publishable.name },
module_version: serialized,
};

const requestOptions = {
method: "POST",
headers: this.headers(),
body: JSON.stringify(body),
};

const request = new Request(endpoint, requestOptions);
const requestId = request.headers.get("x-substrate-request-id");
const apiResponse = await fetch(request);

if (apiResponse.ok) {
const json = await apiResponse.json();
const res = new SubstrateResponse(request, apiResponse, json);
return asSubstratePublishedModuleResponse(res);
} else {
throw new SubstrateError(
`[Request failed] status=${apiResponse.status} statusText=${apiResponse.statusText} requestId=${requestId}`,
);
}
},
};
}
17 changes: 17 additions & 0 deletions src/SubstrateResponse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AnyNode, NodeOutput } from "substrate/Nodes";
import { NodeError } from "substrate/Error";
import { PublishedModule } from "substrate/Module";

/**
* Response to a run request.
Expand Down Expand Up @@ -39,3 +40,19 @@ export class SubstrateResponse {
return node.output() as NodeOutput<T>;
}
}

// TODO: create an alternate SubstrateResponse for non-compose responses
// For now using a type assertions and modifying the object.
export type SubstratePublishModuleResponse = Omit<
SubstrateResponse,
"get" | "getError"
> & { json: PublishedModule };
export const asSubstratePublishedModuleResponse = (
res: SubstrateResponse,
): SubstratePublishModuleResponse => {
// @ts-ignore
delete res.get;
// @ts-ignore
delete res.getError;
return res as SubstratePublishModuleResponse;
};

0 comments on commit 5863aba

Please sign in to comment.