| 209 | } |
| 210 | |
| 211 | export default class API { |
| 212 | apiRoot: string; |
| 213 | graphQLAPIRoot: string; |
| 214 | token: string | boolean; |
| 215 | branch: string; |
| 216 | useOpenAuthoring?: boolean; |
| 217 | repo: string; |
| 218 | repoURL: string; |
| 219 | commitAuthor?: CommitAuthor; |
| 220 | squashMerges: boolean; |
| 221 | initialWorkflowStatus: string; |
| 222 | cmsLabelPrefix: string; |
| 223 | requestFunction?: (req: ApiRequest) => Promise<Response>; |
| 224 | |
| 225 | graphQLClient?: ApolloClient<NormalizedCacheObject>; |
| 226 | |
| 227 | constructor(config: Config) { |
| 228 | this.apiRoot = config.apiRoot || 'https://gitlab.com/api/v4'; |
| 229 | this.graphQLAPIRoot = config.graphQLAPIRoot || 'https://gitlab.com/api/graphql'; |
| 230 | this.token = config.token || false; |
| 231 | this.requestFunction = config.requestFunction; |
| 232 | this.branch = config.branch || 'master'; |
| 233 | this.repo = config.repo || ''; |
| 234 | this.repoURL = `/projects/${encodeURIComponent(this.repo)}`; |
| 235 | this.squashMerges = config.squashMerges; |
| 236 | this.initialWorkflowStatus = config.initialWorkflowStatus; |
| 237 | this.cmsLabelPrefix = config.cmsLabelPrefix; |
| 238 | if (config.useGraphQL === true) { |
| 239 | this.graphQLClient = this.getApolloClient(); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | getApolloClient() { |
| 244 | const authLink = setContext((_, { headers }) => { |
| 245 | return { |
| 246 | headers: { |
| 247 | 'Content-Type': 'application/json; charset=utf-8', |
| 248 | ...headers, |
| 249 | authorization: this.token ? `Bearer ${this.token}` : '', |
| 250 | }, |
| 251 | }; |
| 252 | }); |
| 253 | const httpLink = createHttpLink({ uri: this.graphQLAPIRoot }); |
| 254 | return new ApolloClient({ |
| 255 | link: authLink.concat(httpLink), |
| 256 | cache: new InMemoryCache(), |
| 257 | defaultOptions: { |
| 258 | watchQuery: { |
| 259 | fetchPolicy: NO_CACHE, |
| 260 | errorPolicy: 'ignore', |
| 261 | }, |
| 262 | query: { |
| 263 | fetchPolicy: NO_CACHE, |
| 264 | errorPolicy: 'all', |
| 265 | }, |
| 266 | }, |
| 267 | }); |
| 268 | } |
nothing calls this directly
no test coverage detected