next-tinacms-s3 10.0.0 → 10.0.2

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/handlers.js CHANGED
@@ -66,7 +66,7 @@ var createMediaHandler = (config, options) => {
66
66
  }
67
67
  switch (req.method) {
68
68
  case "GET":
69
- if (req.url.startsWith("/api/s3/media/upload_url")) {
69
+ if (req.query.key) {
70
70
  const expiresIn = req.query.expiresIn && Number(req.query.expiresIn) || 3600;
71
71
  const s3_key = req.query.key ? Array.isArray(req.query.key) ? req.query.key[0] : req.query.key : null;
72
72
  if (!s3_key) {
@@ -82,9 +82,8 @@ var createMediaHandler = (config, options) => {
82
82
  client
83
83
  );
84
84
  return res.json({ signedUrl, src: cdnUrl + s3_key });
85
- } else {
86
- return listMedia(req, res, client, bucket, mediaRoot, cdnUrl);
87
85
  }
86
+ return listMedia(req, res, client, bucket, mediaRoot, cdnUrl);
88
87
  case "DELETE":
89
88
  return deleteAsset(req, res, client, bucket);
90
89
  default:
package/dist/index.js CHANGED
@@ -54,10 +54,19 @@
54
54
  return fetch(input, init);
55
55
  };
56
56
  this.accept = tinacms.DEFAULT_MEDIA_UPLOAD_TYPES;
57
+ this.basePath = "";
57
58
  this.parse = (img) => {
58
59
  return img.src;
59
60
  };
60
61
  }
62
+ fetchWithBasePath(path, init) {
63
+ const fullPath = this.getFullPath(path);
64
+ const normalizedPath = fullPath.startsWith("/") ? fullPath : `/${fullPath}`;
65
+ return this.fetchFunction(normalizedPath, init);
66
+ }
67
+ getFullPath(path) {
68
+ return `${this.basePath}${path}`;
69
+ }
61
70
  async persist(media) {
62
71
  const newFiles = [];
63
72
  for (const item of media) {
@@ -66,7 +75,7 @@
66
75
  directory = directory.substr(0, directory.length - 1);
67
76
  }
68
77
  const path = `${directory && directory !== "/" ? `${directory}/${item.file.name}` : item.file.name}`;
69
- const res = await this.fetchFunction(
78
+ const res = await this.fetchWithBasePath(
70
79
  `/api/s3/media/upload_url?key=${path}`,
71
80
  {
72
81
  method: "GET"
@@ -116,13 +125,16 @@
116
125
  return newFiles;
117
126
  }
118
127
  async delete(media) {
119
- await this.fetchFunction(`/api/s3/media/${encodeURIComponent(media.id)}`, {
120
- method: "DELETE"
121
- });
128
+ await this.fetchWithBasePath(
129
+ `/api/s3/media/${encodeURIComponent(media.id)}`,
130
+ {
131
+ method: "DELETE"
132
+ }
133
+ );
122
134
  }
123
135
  async list(options) {
124
136
  const query = this.buildQuery(options);
125
- const response = await this.fetchFunction("/api/s3/media" + query);
137
+ const response = await this.fetchWithBasePath("/api/s3/media" + query);
126
138
  if (response.status == 401) {
127
139
  throw E_UNAUTHORIZED;
128
140
  }
@@ -147,8 +159,11 @@
147
159
  }
148
160
  class TinaCloudS3MediaStore extends S3MediaStore {
149
161
  constructor(client) {
162
+ var _a;
150
163
  super();
151
164
  this.client = client;
165
+ const basePath = (_a = this.client.schema.config.config.build) == null ? void 0 : _a.basePath;
166
+ this.basePath = basePath || "";
152
167
  this.fetchFunction = async (input, init) => {
153
168
  try {
154
169
  const url = input.toString();
package/dist/index.mjs CHANGED
@@ -51,10 +51,19 @@ class S3MediaStore {
51
51
  return fetch(input, init);
52
52
  };
53
53
  this.accept = DEFAULT_MEDIA_UPLOAD_TYPES;
54
+ this.basePath = "";
54
55
  this.parse = (img) => {
55
56
  return img.src;
56
57
  };
57
58
  }
59
+ fetchWithBasePath(path, init) {
60
+ const fullPath = this.getFullPath(path);
61
+ const normalizedPath = fullPath.startsWith("/") ? fullPath : `/${fullPath}`;
62
+ return this.fetchFunction(normalizedPath, init);
63
+ }
64
+ getFullPath(path) {
65
+ return `${this.basePath}${path}`;
66
+ }
58
67
  async persist(media) {
59
68
  const newFiles = [];
60
69
  for (const item of media) {
@@ -63,7 +72,7 @@ class S3MediaStore {
63
72
  directory = directory.substr(0, directory.length - 1);
64
73
  }
65
74
  const path = `${directory && directory !== "/" ? `${directory}/${item.file.name}` : item.file.name}`;
66
- const res = await this.fetchFunction(
75
+ const res = await this.fetchWithBasePath(
67
76
  `/api/s3/media/upload_url?key=${path}`,
68
77
  {
69
78
  method: "GET"
@@ -113,13 +122,16 @@ class S3MediaStore {
113
122
  return newFiles;
114
123
  }
115
124
  async delete(media) {
116
- await this.fetchFunction(`/api/s3/media/${encodeURIComponent(media.id)}`, {
117
- method: "DELETE"
118
- });
125
+ await this.fetchWithBasePath(
126
+ `/api/s3/media/${encodeURIComponent(media.id)}`,
127
+ {
128
+ method: "DELETE"
129
+ }
130
+ );
119
131
  }
120
132
  async list(options) {
121
133
  const query = this.buildQuery(options);
122
- const response = await this.fetchFunction("/api/s3/media" + query);
134
+ const response = await this.fetchWithBasePath("/api/s3/media" + query);
123
135
  if (response.status == 401) {
124
136
  throw E_UNAUTHORIZED;
125
137
  }
@@ -144,8 +156,11 @@ class S3MediaStore {
144
156
  }
145
157
  class TinaCloudS3MediaStore extends S3MediaStore {
146
158
  constructor(client) {
159
+ var _a;
147
160
  super();
148
161
  this.client = client;
162
+ const basePath = (_a = this.client.schema.config.config.build) == null ? void 0 : _a.basePath;
163
+ this.basePath = basePath || "";
149
164
  this.fetchFunction = async (input, init) => {
150
165
  try {
151
166
  const url = input.toString();
@@ -5,6 +5,9 @@ import type { Media, MediaList, MediaListOptions, MediaStore, MediaUploadOptions
5
5
  export declare class S3MediaStore implements MediaStore {
6
6
  fetchFunction: (input: RequestInfo, init?: RequestInit) => Promise<Response>;
7
7
  accept: string;
8
+ basePath: string;
9
+ protected fetchWithBasePath(path: string, init?: RequestInit): Promise<Response>;
10
+ protected getFullPath(path: string): string;
8
11
  persist(media: MediaUploadOptions[]): Promise<Media[]>;
9
12
  delete(media: Media): Promise<void>;
10
13
  list(options: MediaListOptions): Promise<MediaList>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-tinacms-s3",
3
- "version": "10.0.0",
3
+ "version": "10.0.2",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.mjs",
6
6
  "files": [
@@ -26,11 +26,11 @@
26
26
  "react": "^18.3.1",
27
27
  "react-dom": "^18.3.1",
28
28
  "typescript": "^5.7.3",
29
- "@tinacms/scripts": "1.3.2",
30
- "tinacms": "2.7.0"
29
+ "@tinacms/scripts": "1.3.3",
30
+ "tinacms": "2.7.2"
31
31
  },
32
32
  "peerDependencies": {
33
- "tinacms": "2.7.0"
33
+ "tinacms": "2.7.2"
34
34
  },
35
35
  "publishConfig": {
36
36
  "registry": "https://registry.npmjs.org"