(integration: Integration.Interface)
| 141 | } |
| 142 | |
| 143 | export function integrationHost(integration: Integration.Interface): PluginContext["integration"] { |
| 144 | return { |
| 145 | reload: integration.reload, |
| 146 | connection: { |
| 147 | active: (id) => integration.connection.active(Integration.ID.make(id)), |
| 148 | resolve: (connection) => |
| 149 | integration.connection.resolve( |
| 150 | connection.type === "credential" ? { ...connection, id: Credential.ID.make(connection.id) } : connection, |
| 151 | ), |
| 152 | }, |
| 153 | transform: (callback) => |
| 154 | integration.transform((draft) => |
| 155 | callback({ |
| 156 | list: () => draft.list().map((value) => ({ id: value.id, name: value.name })), |
| 157 | get: (id) => { |
| 158 | const value = draft.get(Integration.ID.make(id)) |
| 159 | return value && { id: value.id, name: value.name } |
| 160 | }, |
| 161 | update: (id, update) => draft.update(Integration.ID.make(id), update), |
| 162 | remove: (id) => draft.remove(Integration.ID.make(id)), |
| 163 | method: { |
| 164 | list: (id) => draft.method.list(Integration.ID.make(id)).map(method), |
| 165 | update: (input) => { |
| 166 | if ("authorize" in input) { |
| 167 | const methodID = Integration.MethodID.make(input.method.id) |
| 168 | const refresh = input.refresh |
| 169 | draft.method.update({ |
| 170 | integrationID: Integration.ID.make(input.integrationID), |
| 171 | method: { ...input.method, id: methodID }, |
| 172 | authorize: (inputs) => |
| 173 | input.authorize(inputs).pipe( |
| 174 | Effect.map((authorization) => { |
| 175 | if (authorization.mode === "auto") { |
| 176 | return { |
| 177 | ...authorization, |
| 178 | callback: authorization.callback.pipe( |
| 179 | Effect.map((credential) => |
| 180 | Credential.OAuth.make({ |
| 181 | ...credential, |
| 182 | methodID: Integration.MethodID.make(credential.methodID), |
| 183 | }), |
| 184 | ), |
| 185 | ), |
| 186 | } |
| 187 | } |
| 188 | return { |
| 189 | ...authorization, |
| 190 | callback: (code: string) => |
| 191 | authorization.callback(code).pipe( |
| 192 | Effect.map((credential) => |
| 193 | Credential.OAuth.make({ |
| 194 | ...credential, |
| 195 | methodID: Integration.MethodID.make(credential.methodID), |
| 196 | }), |
| 197 | ), |
| 198 | ), |
| 199 | } |
| 200 | }), |
no test coverage detected