-
Notifications
You must be signed in to change notification settings - Fork 11
/
sources.ts
152 lines (131 loc) · 4.84 KB
/
sources.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import * as t from "io-ts/lib/index.js";
import * as G from "@marlowe.io/language-core-v1/guards";
import * as ObjG from "@marlowe.io/marlowe-object/guards";
import * as E from "fp-ts/lib/Either.js";
import { pipe } from "fp-ts/lib/function.js";
import { formatValidationErrors } from "jsonbigint-io-ts-reporters";
import { Contract } from "@marlowe.io/language-core-v1";
import {
Label,
ContractSourceId,
ContractSourceIdGuard,
ContractBundleList,
stripAnnotations,
} from "@marlowe.io/marlowe-object";
import { BundleList } from "@marlowe.io/marlowe-object/bundle-list";
import { AxiosInstance } from "axios";
import { assertGuardEqual, proxy } from "@marlowe.io/adapter/io-ts";
/**
* Request options for the {@link index.RestClient#createContractSources | Create contract sources } endpoint
* @category Endpoint : Create contract sources
*/
export interface CreateContractSourcesRequest {
bundle: ContractBundleList<unknown>;
}
export const CreateContractSourcesRequestGuard: t.Type<CreateContractSourcesRequest> = t.type({
bundle: ObjG.ContractBundleList,
});
export interface CreateContractSourcesResponse {
contractSourceId: ContractSourceId;
intermediateIds: {
[key: Label]: ContractSourceId;
};
}
const CreateContractSourcesResponseGuard: t.Type<CreateContractSourcesResponse> = t.type({
contractSourceId: ContractSourceIdGuard,
intermediateIds: t.record(t.string, ContractSourceIdGuard),
});
export const createContractSources = (axiosInstance: AxiosInstance) => {
return async (main: Label, bundle: BundleList<unknown>): Promise<CreateContractSourcesResponse> => {
const response = await axiosInstance.post("/contracts/sources", stripAnnotations(bundle), {
params: { main },
});
return pipe(
CreateContractSourcesResponseGuard.decode(response.data),
E.match(
(e) => {
throw formatValidationErrors(e);
},
(v) => v
)
);
};
};
export interface GetContractBySourceIdRequest {
contractSourceId: ContractSourceId;
expand?: boolean;
}
export const GetContractBySourceIdRequestGuard = assertGuardEqual(
proxy<GetContractBySourceIdRequest>(),
t.intersection([t.type({ contractSourceId: ContractSourceIdGuard }), t.partial({ expand: t.boolean })])
);
export type GetContractBySourceIdResponse = Contract;
const GetContractBySourceIdResponseGuard: t.Type<GetContractBySourceIdResponse> = G.Contract;
export const getContractSourceById =
(axiosInstance: AxiosInstance) =>
async ({ contractSourceId, expand }: GetContractBySourceIdRequest): Promise<GetContractBySourceIdResponse> => {
const response = await axiosInstance.get(`/contracts/sources/${encodeURIComponent(contractSourceId)}`, {
params: { expand },
});
return pipe(
GetContractBySourceIdResponseGuard.decode(response.data),
E.match(
(e) => {
throw formatValidationErrors(e);
},
(e) => e
)
);
};
export interface GetContractSourceAdjacencyRequest {
contractSourceId: ContractSourceId;
}
export const GetContractSourceAdjacencyRequestGuard: t.Type<GetContractSourceAdjacencyRequest> = t.type({
contractSourceId: ContractSourceIdGuard,
});
export interface GetContractSourceAdjacencyResponse {
results: ContractSourceId[];
}
const GetContractSourceAdjacencyResponseGuard: t.Type<GetContractSourceAdjacencyResponse> = t.type({
results: t.array(ContractSourceIdGuard),
});
export const getContractSourceAdjacency =
(axiosInstance: AxiosInstance) =>
async ({ contractSourceId }: GetContractSourceAdjacencyRequest): Promise<GetContractSourceAdjacencyResponse> => {
const response = await axiosInstance.get(`/contracts/sources/${encodeURIComponent(contractSourceId)}/adjacency`);
return pipe(
GetContractSourceAdjacencyResponseGuard.decode(response.data),
E.match(
(e) => {
throw formatValidationErrors(e);
},
(e) => e
)
);
};
export interface GetContractSourceClosureRequest {
contractSourceId: ContractSourceId;
}
export const GetContractSourceClosureRequestGuard: t.Type<GetContractSourceClosureRequest> = t.type({
contractSourceId: ContractSourceIdGuard,
});
export interface GetContractSourceClosureResponse {
results: ContractSourceId[];
}
const GetContractSourceClosureResponseGuard: t.Type<GetContractSourceClosureResponse> = t.type({
results: t.array(ContractSourceIdGuard),
});
export const getContractSourceClosure =
(axiosInstance: AxiosInstance) =>
async ({ contractSourceId }: GetContractSourceClosureRequest): Promise<GetContractSourceClosureResponse> => {
const response = await axiosInstance.get(`/contracts/sources/${encodeURIComponent(contractSourceId)}/closure`);
return pipe(
GetContractSourceClosureResponseGuard.decode(response.data),
E.match(
(e) => {
throw formatValidationErrors(e);
},
(e) => e
)
);
};