Ben Irvin 13a2f8b246
feat: Upgrade to Apollo v4
* feat: update and make build work

BREAKING CHANGE: Update from 'apollo-server-koa' to '@apollo/server' and '@as-integrations/koa'

* chore: fix comments

* chore: upgrade graphql-upload package

* chore: fix for body type unknown

* chore: remove old comment

* chore: clean up error handling

* chore: fix comment

* fix: http status codes for input validation errors

* fix: remove unused import

* fix: remove accidental bodyparser

* fix: add new required header to tests

* chore: standardize directive key names to be kebab-case

* test: add some extra message validation

* chore: remove devdep for koa-cors typings

* fix: add unknown error name

* fix: yarn.lock

* fix: add typings

* fix: typings

* fix: typings again

* fix: remove unused imports

* chore: remove unused import

* chore: move playground check to a service

* fix: package imports and versions

* chore: fix yarn.lock

* chore: fix types

* chore: clean up koa typings

* chore: koa typing cleanup

* chore: cleanup koa typings

* chore: more koa type cleanup

* chore: revert missing imports

* chore: cleanup koa typings

* chore: update yarn.lock
2024-01-15 14:54:58 +01:00

109 lines
2.4 KiB
JavaScript

'use strict';
const { clone, has, concat, isNil } = require('lodash/fp');
const qs = require('qs');
const request = require('supertest');
const { createUtils } = require('./utils');
const createAgent = (strapi, initialState = {}) => {
const state = clone(initialState);
const utils = createUtils(strapi);
const agent = (options) => {
const { method, url, body, formData, qs: queryString, headers } = options;
const supertestAgent = request.agent(strapi.server.httpServer);
if (has('token', state)) {
supertestAgent.auth(state.token, { type: 'bearer' });
}
if (headers) {
supertestAgent.set(headers);
} else if (has('headers', state)) {
supertestAgent.set(state.headers);
}
const fullUrl = concat(state.urlPrefix, url).join('');
const rq = supertestAgent[method.toLowerCase()](fullUrl);
if (queryString) {
rq.query(qs.stringify(queryString));
}
if (body) {
rq.send(body);
}
if (formData) {
const attachFieldToRequest = (field) => rq.field(field, formData[field]);
Object.keys(formData).forEach(attachFieldToRequest);
}
if (isNil(formData)) {
rq.type('application/json');
}
return rq;
};
const createShorthandMethod =
(method) =>
(url, options = {}) => {
return agent({ ...options, url, method });
};
Object.assign(agent, {
assignState(newState) {
Object.assign(state, newState);
return agent;
},
setURLPrefix(path) {
return this.assignState({ urlPrefix: path });
},
setToken(token) {
return this.assignState({ token });
},
setLoggedUser(loggedUser) {
return this.assignState({ loggedUser });
},
setHeaders(headers) {
return this.assignState({ headers });
},
getLoggedUser() {
return state.loggedUser;
},
async login(userInfo) {
const { token, user } = await utils.login(userInfo);
this.setToken(token).setLoggedUser(user);
return agent;
},
async registerOrLogin(userCredentials) {
const { token, user } = await utils.registerOrLogin(userCredentials);
this.setToken(token).setLoggedUser(user);
return agent;
},
get: createShorthandMethod('GET'),
post: createShorthandMethod('POST'),
put: createShorthandMethod('PUT'),
delete: createShorthandMethod('DELETE'),
});
return agent;
};
module.exports = {
createAgent,
};