@envelop/preload-assets 4.0.0-alpha-d0d0776.0 → 4.0.0-alpha-ccec0fc.0

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.
package/README.md CHANGED
@@ -12,9 +12,9 @@ yarn add @envelop/preload-assets
12
12
  ```
13
13
 
14
14
  ```ts
15
- import { envelop } from '@envelop/core';
16
- import { usePreloadAssets } from '@envelop/preload-asset';
17
- import { makeExecutableSchema } from 'graphql';
15
+ import { envelop } from '@envelop/core'
16
+ import { usePreloadAssets } from '@envelop/preload-asset'
17
+ import { makeExecutableSchema } from '@graphql-tools/graphql'
18
18
 
19
19
  const schema = makeExecutableSchema({
20
20
  typeDefs: /* GraphQL */ `
@@ -25,17 +25,17 @@ const schema = makeExecutableSchema({
25
25
  resolvers: {
26
26
  Query: {
27
27
  imageUrl: (_: unknown, __: unknown, context: any) => {
28
- const imageUrl = 'https://localhost/some-asset.png';
29
- context.registerPreloadAsset(imageUrl);
30
- return Promise.resolve(imageUrl);
31
- },
32
- },
33
- },
34
- });
28
+ const imageUrl = 'https://localhost/some-asset.png'
29
+ context.registerPreloadAsset(imageUrl)
30
+ return Promise.resolve(imageUrl)
31
+ }
32
+ }
33
+ }
34
+ })
35
35
 
36
36
  const getEnveloped = envelop({
37
- plugins: [usePreloadAssets()],
38
- });
37
+ plugins: [usePreloadAssets()]
38
+ })
39
39
  ```
40
40
 
41
41
  **Example response**
@@ -54,27 +54,27 @@ const getEnveloped = envelop({
54
54
  **Example client prefetch logic**
55
55
 
56
56
  ```ts
57
- const preloadAsset = (url) => {
58
- var request = new XMLHttpRequest();
59
- request.open('GET', url);
60
- request.responseType = 'blob';
57
+ const preloadAsset = url => {
58
+ var request = new XMLHttpRequest()
59
+ request.open('GET', url)
60
+ request.responseType = 'blob'
61
61
  request.onload = () => {
62
- if (request.status !== 200 ) {
63
- console.error((new Error(`Image preload failed; error code '${request.statusText}'.`));
62
+ if (request.status !== 200) {
63
+ console.error(new Error(`Image preload failed; error code '${request.statusText}'.`))
64
64
  }
65
- };
65
+ }
66
66
  request.onerror = () => {
67
- console.error(new Error(`There was a network error while preloading '${url}'.`));
68
- };
69
- request.send();
67
+ console.error(new Error(`There was a network error while preloading '${url}'.`))
68
+ }
69
+ request.send()
70
70
  }
71
71
 
72
72
  // call this function with the execution result within your network layer.
73
- const onExecutionResult = (result) => {
73
+ const onExecutionResult = result => {
74
74
  if (Array.isArray(result?.extensions?.preloadAssets)) {
75
- result.extension.preloadAssets.forEach((url) => {
76
- preloadAsset(url);
77
- });
75
+ result.extension.preloadAssets.forEach(url => {
76
+ preloadAsset(url)
77
+ })
78
78
  }
79
- };
79
+ }
80
80
  ```
@@ -1,22 +1,21 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- const core = require('@envelop/core');
6
-
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.usePreloadAssets = void 0;
4
+ const core_1 = require("@envelop/core");
7
5
  const usePreloadAssets = (opts) => ({
8
6
  onExecute: ({ extendContext, args }) => {
7
+ var _a, _b;
9
8
  const assets = new Set();
10
9
  extendContext({
11
10
  registerPreloadAsset: (assetUrl) => assets.add(assetUrl),
12
11
  });
13
- if (opts?.shouldPreloadAssets?.(args.contextValue) ?? true) {
12
+ if ((_b = (_a = opts === null || opts === void 0 ? void 0 : opts.shouldPreloadAssets) === null || _a === void 0 ? void 0 : _a.call(opts, args.contextValue)) !== null && _b !== void 0 ? _b : true) {
14
13
  return {
15
14
  onExecuteDone: payload => {
16
15
  if (!assets.size) {
17
16
  return;
18
17
  }
19
- return core.handleStreamOrSingleExecutionResult(payload, ({ result, setResult }) => {
18
+ return (0, core_1.handleStreamOrSingleExecutionResult)(payload, ({ result, setResult }) => {
20
19
  setResult({
21
20
  ...result,
22
21
  extensions: {
@@ -31,5 +30,4 @@ const usePreloadAssets = (opts) => ({
31
30
  return undefined;
32
31
  },
33
32
  });
34
-
35
33
  exports.usePreloadAssets = usePreloadAssets;
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
@@ -1,12 +1,12 @@
1
1
  import { handleStreamOrSingleExecutionResult } from '@envelop/core';
2
-
3
- const usePreloadAssets = (opts) => ({
2
+ export const usePreloadAssets = (opts) => ({
4
3
  onExecute: ({ extendContext, args }) => {
4
+ var _a, _b;
5
5
  const assets = new Set();
6
6
  extendContext({
7
7
  registerPreloadAsset: (assetUrl) => assets.add(assetUrl),
8
8
  });
9
- if (opts?.shouldPreloadAssets?.(args.contextValue) ?? true) {
9
+ if ((_b = (_a = opts === null || opts === void 0 ? void 0 : opts.shouldPreloadAssets) === null || _a === void 0 ? void 0 : _a.call(opts, args.contextValue)) !== null && _b !== void 0 ? _b : true) {
10
10
  return {
11
11
  onExecuteDone: payload => {
12
12
  if (!assets.size) {
@@ -27,5 +27,3 @@ const usePreloadAssets = (opts) => ({
27
27
  return undefined;
28
28
  },
29
29
  });
30
-
31
- export { usePreloadAssets };
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@envelop/preload-assets",
3
- "version": "4.0.0-alpha-d0d0776.0",
3
+ "version": "4.0.0-alpha-ccec0fc.0",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
- "@envelop/core": "3.0.0-alpha-d0d0776.0",
7
- "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
6
+ "@envelop/core": "3.0.0-alpha-ccec0fc.0",
7
+ "@graphql-tools/graphql": "0.1.0-alpha-e7752ba5.0"
8
8
  },
9
+ "dependencies": {},
9
10
  "repository": {
10
11
  "type": "git",
11
12
  "url": "https://github.com/n1ru4l/envelop.git",
@@ -13,33 +14,42 @@
13
14
  },
14
15
  "author": "Dotan Simha <dotansimha@gmail.com>",
15
16
  "license": "MIT",
16
- "main": "index.js",
17
- "module": "index.mjs",
18
- "typings": "index.d.ts",
17
+ "main": "cjs/index.js",
18
+ "module": "esm/index.js",
19
+ "typings": "typings/index.d.ts",
19
20
  "typescript": {
20
- "definition": "index.d.ts"
21
+ "definition": "typings/index.d.ts"
21
22
  },
23
+ "type": "module",
22
24
  "exports": {
23
25
  ".": {
24
26
  "require": {
25
- "default": "./index.js",
26
- "types": "./index.d.ts"
27
+ "types": "./typings/index.d.ts",
28
+ "default": "./cjs/index.js"
27
29
  },
28
30
  "import": {
29
- "default": "./index.mjs",
30
- "types": "./index.d.ts"
31
+ "types": "./typings/index.d.ts",
32
+ "default": "./esm/index.js"
33
+ },
34
+ "default": {
35
+ "types": "./typings/index.d.ts",
36
+ "default": "./esm/index.js"
31
37
  }
32
38
  },
33
39
  "./*": {
34
40
  "require": {
35
- "default": "./*.js",
36
- "types": "./*.d.ts"
41
+ "types": "./typings/*.d.ts",
42
+ "default": "./cjs/*.js"
37
43
  },
38
44
  "import": {
39
- "default": "./*.mjs",
40
- "types": "./*.d.ts"
45
+ "types": "./typings/*.d.ts",
46
+ "default": "./esm/*.js"
47
+ },
48
+ "default": {
49
+ "types": "./typings/*.d.ts",
50
+ "default": "./esm/*.js"
41
51
  }
42
52
  },
43
53
  "./package.json": "./package.json"
44
54
  }
45
- }
55
+ }
@@ -2,4 +2,4 @@ import { Plugin } from '@envelop/core';
2
2
  export declare type UsePreloadAssetsOpts = {
3
3
  shouldPreloadAssets?: (context: unknown) => boolean;
4
4
  };
5
- export declare const usePreloadAssets: (opts?: UsePreloadAssetsOpts | undefined) => Plugin;
5
+ export declare const usePreloadAssets: (opts?: UsePreloadAssetsOpts) => Plugin;