@jahia/cypress 1.1.5 → 1.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,6 +3,7 @@
3
3
  exports.__esModule = true;
4
4
  exports.apolloClient = void 0;
5
5
  var core_1 = require("@apollo/client/core");
6
+ var links_1 = require("./links");
6
7
  var apolloClient = function (authMethod, options) {
7
8
  if (options === void 0) { options = {
8
9
  log: true,
@@ -18,11 +19,9 @@ var apolloClient = function (authMethod, options) {
18
19
  else if (authMethod.username !== undefined && authMethod.password !== undefined) {
19
20
  headers.authorization = "Basic " + btoa(authMethod.username + ':' + authMethod.password);
20
21
  }
22
+ var links = [links_1.uploadLink, links_1.FormDataHttpLink(Cypress.config().baseUrl, headers)];
21
23
  var client = new core_1.ApolloClient({
22
- link: new core_1.HttpLink({
23
- uri: Cypress.config().baseUrl + "/modules/graphql",
24
- headers: headers
25
- }),
24
+ link: core_1.from(links),
26
25
  cache: new core_1.InMemoryCache(),
27
26
  defaultOptions: {
28
27
  query: {
@@ -0,0 +1,3 @@
1
+ import { HttpLink } from '@apollo/client/link/http';
2
+ export declare const FormDataHttpLink: (baseUrl: string, headers: Object) => HttpLink;
3
+ export declare const uploadLink: import("@apollo/client/core").ApolloLink;
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __importDefault = (this && this.__importDefault) || function (mod) {
14
+ return (mod && mod.__esModule) ? mod : { "default": mod };
15
+ };
16
+ exports.__esModule = true;
17
+ exports.uploadLink = exports.FormDataHttpLink = void 0;
18
+ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
19
+ var http_1 = require("@apollo/client/link/http");
20
+ var cross_fetch_1 = __importDefault(require("cross-fetch"));
21
+ var context_1 = require("@apollo/client/link/context");
22
+ var FormDataHttpLink = function (baseUrl, headers) {
23
+ return new http_1.HttpLink({
24
+ uri: baseUrl + "/modules/graphql",
25
+ headers: headers,
26
+ fetch: function (uri, fetcherOptions) {
27
+ var options = __assign({}, fetcherOptions);
28
+ if (options.formData) {
29
+ var formData_1 = options.formData;
30
+ var body_1 = JSON.parse(options.body.toString());
31
+ if (Array.isArray(body_1)) {
32
+ formData_1.append('query', options.body.toString());
33
+ }
34
+ else {
35
+ Object.keys(body_1).forEach(function (k) {
36
+ return formData_1.append(k, typeof body_1[k] === 'string' ? body_1[k] : JSON.stringify(body_1[k]));
37
+ });
38
+ }
39
+ fetcherOptions.body = formData_1;
40
+ delete fetcherOptions.headers['content-type'];
41
+ return cross_fetch_1["default"](uri, fetcherOptions);
42
+ }
43
+ return cross_fetch_1["default"](uri, fetcherOptions);
44
+ }
45
+ });
46
+ };
47
+ exports.FormDataHttpLink = FormDataHttpLink;
48
+ exports.uploadLink = context_1.setContext(function (operation, _a) {
49
+ var fetchOptions = _a.fetchOptions;
50
+ var variables = operation.variables;
51
+ var fileFound = false;
52
+ var formData = new FormData();
53
+ var id = Math.random().toString(36);
54
+ // Search for File objects on the request and set it as formData
55
+ Object.keys(variables).forEach(function (k) {
56
+ var variable = variables[k];
57
+ if (variable instanceof File) {
58
+ formData.append(id, variable);
59
+ variables[k] = id;
60
+ fileFound = true;
61
+ }
62
+ });
63
+ if (fileFound) {
64
+ return {
65
+ fetchOptions: __assign(__assign({}, fetchOptions), { formData: formData })
66
+ };
67
+ }
68
+ else {
69
+ return {
70
+ fetchOptions: __assign({}, fetchOptions)
71
+ };
72
+ }
73
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jahia/cypress",
3
- "version": "1.1.5",
3
+ "version": "1.1.6",
4
4
  "scripts": {
5
5
  "build": "tsc",
6
6
  "lint": "eslint src -c .eslintrc.json --ext .ts"
@@ -14,6 +14,7 @@
14
14
  "cypress": "^8.3.1",
15
15
  "eslint": "^7.32.0",
16
16
  "eslint-plugin-cypress": "^2.11.3",
17
+ "cross-fetch": "^3.1.5",
17
18
  "typescript": "^4.3.5"
18
19
  },
19
20
  "dependencies": {
@@ -1,6 +1,7 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-namespace */
2
2
 
3
- import {ApolloClient, HttpLink, InMemoryCache, NormalizedCacheObject} from '@apollo/client/core'
3
+ import {ApolloClient, from, HttpLink, InMemoryCache, NormalizedCacheObject} from '@apollo/client/core'
4
+ import {FormDataHttpLink, uploadLink} from './links'
4
5
 
5
6
  interface AuthMethod {
6
7
  token?: string
@@ -34,11 +35,10 @@ export const apolloClient = function (authMethod?: AuthMethod, options: ApolloCl
34
35
  headers.authorization = `Basic ${btoa(authMethod.username + ':' + authMethod.password)}`
35
36
  }
36
37
 
38
+ const links = [ uploadLink, FormDataHttpLink(Cypress.config().baseUrl, headers)]
39
+
37
40
  const client = new ApolloClient({
38
- link: new HttpLink({
39
- uri: `${Cypress.config().baseUrl}/modules/graphql`,
40
- headers,
41
- }),
41
+ link: from(links),
42
42
  cache: new InMemoryCache(),
43
43
  defaultOptions: {
44
44
  query: {
@@ -0,0 +1,64 @@
1
+ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
2
+ import { HttpLink } from '@apollo/client/link/http'
3
+ import fetch from 'cross-fetch'
4
+ import { setContext } from '@apollo/client/link/context'
5
+
6
+ interface ApolloRequestInit extends RequestInit {
7
+ formData?: FormData
8
+ }
9
+
10
+ export const FormDataHttpLink = (baseUrl: string, headers: Object) => {
11
+ return new HttpLink({
12
+ uri: `${baseUrl}/modules/graphql`,
13
+ headers,
14
+ fetch: (uri, fetcherOptions) => {
15
+ const options: ApolloRequestInit = { ...fetcherOptions }
16
+ if (options.formData) {
17
+ const formData = options.formData
18
+ const body = JSON.parse(options.body.toString())
19
+ if (Array.isArray(body)) {
20
+ formData.append('query', options.body.toString())
21
+ } else {
22
+ Object.keys(body).forEach((k) =>
23
+ formData.append(k, typeof body[k] === 'string' ? body[k] : JSON.stringify(body[k])),
24
+ )
25
+ }
26
+
27
+ fetcherOptions.body = formData
28
+ delete fetcherOptions.headers['content-type']
29
+ return fetch(uri, fetcherOptions)
30
+ }
31
+ return fetch(uri, fetcherOptions)
32
+ },
33
+ })
34
+ }
35
+
36
+ export const uploadLink = setContext((operation, { fetchOptions }) => {
37
+ const { variables } = operation
38
+ let fileFound = false
39
+ const formData = new FormData()
40
+ const id = Math.random().toString(36)
41
+ // Search for File objects on the request and set it as formData
42
+ Object.keys(variables).forEach(function (k) {
43
+ const variable = variables[k]
44
+ if (variable instanceof File) {
45
+ formData.append(id, variable)
46
+ variables[k] = id
47
+ fileFound = true
48
+ }
49
+ })
50
+ if (fileFound) {
51
+ return {
52
+ fetchOptions: {
53
+ ...fetchOptions,
54
+ formData: formData,
55
+ },
56
+ }
57
+ } else {
58
+ return {
59
+ fetchOptions: {
60
+ ...fetchOptions,
61
+ },
62
+ }
63
+ }
64
+ })