next-tinacms-dos 1.3.1 → 1.3.3

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/dist/index.mjs ADDED
@@ -0,0 +1,130 @@
1
+ import { DEFAULT_MEDIA_UPLOAD_TYPES } from "tinacms";
2
+ class MediaListError extends Error {
3
+ constructor(config) {
4
+ super(config.message);
5
+ this.ERR_TYPE = "MediaListError";
6
+ this.title = config.title;
7
+ this.docsLink = config.docsLink;
8
+ }
9
+ }
10
+ const E_DEFAULT = new MediaListError({
11
+ title: "An Error Occurred",
12
+ message: "Something went wrong fetching your media from Digital Ocean Space.",
13
+ docsLink: "https://tina.io/packages/next-tinacms-dos"
14
+ });
15
+ const E_UNAUTHORIZED = new MediaListError({
16
+ title: "Unauthorized",
17
+ message: "You don't have access to this resource.",
18
+ docsLink: "https://tina.io/packages/next-tinacms-dos"
19
+ });
20
+ const E_CONFIG = new MediaListError({
21
+ title: "Missing Credentials",
22
+ message: "Unable to connect to Digital Ocean Space because one or more environment variables are missing.",
23
+ docsLink: "https://tina.io/docs/media-dos/"
24
+ });
25
+ const E_KEY_FAIL = new MediaListError({
26
+ title: "Bad Credentials",
27
+ message: "Unable to connect to Digital Ocean Space because one or more environment variables are misconfigured.",
28
+ docsLink: "https://tina.io/docs/media-dos/"
29
+ });
30
+ const E_BAD_ROUTE = new MediaListError({
31
+ title: "Bad Route",
32
+ message: "The Digital Ocean Space API route is missing or misconfigured.",
33
+ docsLink: "https://tina.io/packages/next-tinacms-dos/#set-up-api-routes"
34
+ });
35
+ const interpretErrorMessage = (message) => {
36
+ switch (message) {
37
+ case "Must supply cloud_name":
38
+ case "Must supply api_key":
39
+ case "Must supply api_secret":
40
+ return E_CONFIG;
41
+ case "unknown api_key":
42
+ return E_KEY_FAIL;
43
+ default:
44
+ return E_DEFAULT;
45
+ }
46
+ };
47
+ class DOSMediaStore {
48
+ constructor() {
49
+ this.fetchFunction = (input, init) => {
50
+ return fetch(input, init);
51
+ };
52
+ this.accept = DEFAULT_MEDIA_UPLOAD_TYPES;
53
+ this.parse = (img) => {
54
+ return img.src;
55
+ };
56
+ }
57
+ async persist(media) {
58
+ const newFiles = [];
59
+ for (const item of media) {
60
+ const { file, directory } = item;
61
+ const formData = new FormData();
62
+ formData.append("file", file);
63
+ formData.append("directory", directory);
64
+ formData.append("filename", file.name);
65
+ const res = await this.fetchFunction(`/api/dos/media`, {
66
+ method: "POST",
67
+ body: formData
68
+ });
69
+ if (res.status != 200) {
70
+ const responseData = await res.json();
71
+ throw new Error(responseData.message);
72
+ }
73
+ const fileRes = await res.json();
74
+ await new Promise((resolve) => {
75
+ setTimeout(resolve, 2e3);
76
+ });
77
+ newFiles.push(fileRes);
78
+ }
79
+ return newFiles;
80
+ }
81
+ async delete(media) {
82
+ await this.fetchFunction(`/api/dos/media/${encodeURIComponent(media.id)}`, {
83
+ method: "DELETE"
84
+ });
85
+ }
86
+ async list(options) {
87
+ const query = this.buildQuery(options);
88
+ const response = await this.fetchFunction("/api/dos/media" + query);
89
+ if (response.status == 401) {
90
+ throw E_UNAUTHORIZED;
91
+ }
92
+ if (response.status == 404) {
93
+ throw E_BAD_ROUTE;
94
+ }
95
+ if (response.status >= 500) {
96
+ const { e } = await response.json();
97
+ const error = interpretErrorMessage(e);
98
+ throw error;
99
+ }
100
+ const { items, offset } = await response.json();
101
+ return {
102
+ items: items.map((item) => item),
103
+ nextOffset: offset
104
+ };
105
+ }
106
+ buildQuery(options) {
107
+ const params = Object.keys(options).filter((key) => options[key] !== "" && options[key] !== void 0).map((key) => `${key}=${options[key]}`).join("&");
108
+ return `?${params}`;
109
+ }
110
+ }
111
+ class TinaCloudDOSMediaStore extends DOSMediaStore {
112
+ constructor(client) {
113
+ super();
114
+ this.client = client;
115
+ this.fetchFunction = async (input, init) => {
116
+ try {
117
+ const url = input.toString();
118
+ const query = `${url.includes("?") ? "&" : "?"}clientID=${client.clientId}`;
119
+ const res = client.fetchWithToken(url + query, init);
120
+ return res;
121
+ } catch (error) {
122
+ console.error(error);
123
+ }
124
+ };
125
+ }
126
+ }
127
+ export {
128
+ DOSMediaStore,
129
+ TinaCloudDOSMediaStore
130
+ };
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "next-tinacms-dos",
3
- "version": "1.3.1",
3
+ "version": "1.3.3",
4
4
  "main": "dist/index.js",
5
- "module": "dist/index.es.js",
5
+ "module": "dist/index.mjs",
6
6
  "files": [
7
7
  "dist"
8
8
  ],
@@ -18,17 +18,21 @@
18
18
  ]
19
19
  },
20
20
  "dependencies": {
21
- "@aws-sdk/client-s3": "3.150.0",
21
+ "@aws-sdk/client-s3": "^3.357.0",
22
22
  "multer": "1.4.5-lts.1"
23
23
  },
24
+ "peerDependencies": {
25
+ "tinacms": "1.5.18"
26
+ },
24
27
  "devDependencies": {
25
- "@tinacms/toolkit": "1.7.2",
26
- "@tinacms/scripts": "1.1.0",
28
+ "@tinacms/scripts": "1.1.2",
27
29
  "@types/crypto-js": "^3.1.47",
28
30
  "@types/js-cookie": "^2.2.6",
29
31
  "@types/node": "^13.13.1",
30
32
  "next": "12.2.4",
31
- "tinacms": "1.5.6",
33
+ "react": "17.0.2",
34
+ "react-dom": "17.0.2",
35
+ "tinacms": "1.5.18",
32
36
  "typescript": "4.3.5"
33
37
  },
34
38
  "publishConfig": {