next-tinacms-cloudinary 0.0.0-003e348-20251023020516
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/LICENSE +176 -0
- package/README.md +143 -0
- package/dist/cloudinary-media-store.d.ts +15 -0
- package/dist/cloudinary-tina-cloud-media-store.d.ts +28 -0
- package/dist/errors.d.ts +21 -0
- package/dist/handlers.d.ts +19 -0
- package/dist/handlers.js +210 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +149 -0
- package/dist/index.mjs +146 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
package/README.md
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
# _next-tinacms-cloudinary_
|
|
2
|
+
|
|
3
|
+
Manage **Cloudinary media assets** in TinaCMS.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
### With Yarn
|
|
8
|
+
```bash
|
|
9
|
+
yarn add next-tinacms-cloudinary
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
### With NPM
|
|
13
|
+
```bash
|
|
14
|
+
npm install next-tinacms-cloudinary
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Connect with Cloudinary
|
|
18
|
+
|
|
19
|
+
You need some credentials provided by Cloudinary to set this up properly. If you do not already have an account, you can (register here)[https://cloudinary.com/users/register/free].
|
|
20
|
+
|
|
21
|
+
**next-tinacms-cloudinary** uses environment variables within the context of a Next.js site to properly access your Cloudinary account.
|
|
22
|
+
|
|
23
|
+
Add the following variables to an `.env` file.
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME=<Your Cloudinary Cloud Name>
|
|
27
|
+
NEXT_PUBLIC_CLOUDINARY_API_KEY=<Your Cloudinary API key>
|
|
28
|
+
CLOUDINARY_API_SECRET=<Your Cloudinary API secret>
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Register the Media Store
|
|
32
|
+
|
|
33
|
+
Now, you can register the Cloudinary Media store with the instance of Tina in your app by passing the `TinaCloudCloudinaryMediaStore` to the `TinaCMS` instance via its `mediaStore` prop.
|
|
34
|
+
|
|
35
|
+
This is also where we can update our `mediaOptions` on the cms object.
|
|
36
|
+
|
|
37
|
+
```tsx
|
|
38
|
+
// Typically in the _app.js file of a Next.js project
|
|
39
|
+
|
|
40
|
+
import dynamic from "next/dynamic";
|
|
41
|
+
import { TinaEditProvider } from "tinacms/dist/edit-state";
|
|
42
|
+
import { Layout } from "../components/layout";
|
|
43
|
+
import { TinaCloudCloudinaryMediaStore } from "next-tinacms-cloudinary";
|
|
44
|
+
const TinaCMS = dynamic(() => import("tinacms"), { ssr: false });
|
|
45
|
+
|
|
46
|
+
const App = ({ Component, pageProps }) => {
|
|
47
|
+
return (
|
|
48
|
+
<>
|
|
49
|
+
<TinaEditProvider
|
|
50
|
+
editMode={
|
|
51
|
+
<TinaCMS
|
|
52
|
+
branch="main"
|
|
53
|
+
clientId={NEXT_PUBLIC_TINA_CLIENT_ID}
|
|
54
|
+
isLocalClient={Boolean(Number(NEXT_PUBLIC_USE_LOCAL_CLIENT))}
|
|
55
|
+
mediaStore={async () => {
|
|
56
|
+
const pack = await import("next-tinacms-cloudinary");
|
|
57
|
+
return pack.TinaCloudCloudinaryMediaStore;
|
|
58
|
+
}}
|
|
59
|
+
{...pageProps}
|
|
60
|
+
>
|
|
61
|
+
{(livePageProps) => (
|
|
62
|
+
<Layout
|
|
63
|
+
rawData={livePageProps}
|
|
64
|
+
data={livePageProps.data?.getGlobalDocument?.data}
|
|
65
|
+
>
|
|
66
|
+
<Component {...livePageProps} />
|
|
67
|
+
</Layout>
|
|
68
|
+
)}
|
|
69
|
+
</TinaCMS>
|
|
70
|
+
}
|
|
71
|
+
>
|
|
72
|
+
<Layout
|
|
73
|
+
rawData={pageProps}
|
|
74
|
+
data={pageProps.data?.getGlobalDocument?.data}
|
|
75
|
+
>
|
|
76
|
+
<Component {...pageProps} />
|
|
77
|
+
</Layout>
|
|
78
|
+
</TinaEditProvider>
|
|
79
|
+
</>
|
|
80
|
+
);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
...
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Set up API routes
|
|
87
|
+
|
|
88
|
+
Set up a new API route in the `pages` directory of your Next.js app, e.g. `pages/api/cloudinary`.
|
|
89
|
+
Then add a new catch all API route for media.
|
|
90
|
+
|
|
91
|
+
Call `createMediaHandler` to set up routes and connect your instance of the Media Store to your Cloudinary account.
|
|
92
|
+
|
|
93
|
+
Import `isAuthorized` from [`@tinacms/auth`](https://github.com/tinacms/tinacms/tree/main/packages/%40tinacms/auth).
|
|
94
|
+
|
|
95
|
+
The `authorized` key will make it so only authorized users within TinaCloud can upload and make media edits.
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
```
|
|
99
|
+
//[...media].tsx
|
|
100
|
+
|
|
101
|
+
import {
|
|
102
|
+
mediaHandlerConfig,
|
|
103
|
+
createMediaHandler,
|
|
104
|
+
} from "next-tinacms-cloudinary/dist/handlers";
|
|
105
|
+
import { isAuthorized } from "@tinacms/auth";
|
|
106
|
+
|
|
107
|
+
export const config = mediaHandlerConfig;
|
|
108
|
+
|
|
109
|
+
export default createMediaHandler({
|
|
110
|
+
cloud_name: process.env.NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME,
|
|
111
|
+
api_key: process.env.NEXT_PUBLIC_CLOUDINARY_API_KEY,
|
|
112
|
+
api_secret: process.env.CLOUDINARY_API_SECRET,
|
|
113
|
+
authorized: async (req, _res) => {
|
|
114
|
+
if (process.env.NEXT_PUBLIC_USE_LOCAL_CLIENT === "1") {
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
try {
|
|
118
|
+
const user = await isAuthorized(req);
|
|
119
|
+
return user && user.verified;
|
|
120
|
+
} catch (e) {
|
|
121
|
+
console.error(e);
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Update Schema
|
|
130
|
+
|
|
131
|
+
Now that the media store is registered and the API route for media set up, let's add an image to your schema.
|
|
132
|
+
|
|
133
|
+
In your `.tina/schema.ts` add a new field for the image, e.g:
|
|
134
|
+
|
|
135
|
+
```
|
|
136
|
+
{
|
|
137
|
+
name: 'hero',
|
|
138
|
+
type: 'image',
|
|
139
|
+
label: 'Hero Image',
|
|
140
|
+
}
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Now, when editing your site, the image field will allow you to connect to your Cloudinary account via the Media Store to manage your media assets.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Media, MediaList, MediaListOptions, MediaStore, MediaUploadOptions } from 'tinacms';
|
|
2
|
+
export type CloudinaryMediaStoreOptions = {
|
|
3
|
+
baseUrl?: string;
|
|
4
|
+
};
|
|
5
|
+
export declare class CloudinaryMediaStore implements MediaStore {
|
|
6
|
+
baseUrl: string;
|
|
7
|
+
constructor(options?: CloudinaryMediaStoreOptions);
|
|
8
|
+
fetchFunction: (input: RequestInfo, init?: RequestInit) => Promise<Response>;
|
|
9
|
+
accept: string;
|
|
10
|
+
persist(media: MediaUploadOptions[]): Promise<Media[]>;
|
|
11
|
+
delete(media: Media): Promise<void>;
|
|
12
|
+
list(options: MediaListOptions): Promise<MediaList>;
|
|
13
|
+
parse: (img: any) => any;
|
|
14
|
+
buildQuery(options: MediaListOptions): string;
|
|
15
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Client } from 'tinacms';
|
|
2
|
+
import type { CloudinaryMediaStoreOptions } from './cloudinary-media-store';
|
|
3
|
+
export declare const createTinaCloudCloudinaryMediaStore: (options?: CloudinaryMediaStoreOptions) => {
|
|
4
|
+
new (client: Client): {
|
|
5
|
+
client: Client;
|
|
6
|
+
baseUrl: string;
|
|
7
|
+
fetchFunction: (input: RequestInfo, init?: RequestInit) => Promise<Response>;
|
|
8
|
+
accept: string;
|
|
9
|
+
persist(media: import("tinacms").MediaUploadOptions[]): Promise<import("tinacms").Media[]>;
|
|
10
|
+
delete(media: import("tinacms").Media): Promise<void>;
|
|
11
|
+
list(options: import("tinacms").MediaListOptions): Promise<import("tinacms").MediaList>;
|
|
12
|
+
parse: (img: any) => any;
|
|
13
|
+
buildQuery(options: import("tinacms").MediaListOptions): string;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
export declare const TinaCloudCloudinaryMediaStore: {
|
|
17
|
+
new (client: Client): {
|
|
18
|
+
client: Client;
|
|
19
|
+
baseUrl: string;
|
|
20
|
+
fetchFunction: (input: RequestInfo, init?: RequestInit) => Promise<Response>;
|
|
21
|
+
accept: string;
|
|
22
|
+
persist(media: import("tinacms").MediaUploadOptions[]): Promise<import("tinacms").Media[]>;
|
|
23
|
+
delete(media: import("tinacms").Media): Promise<void>;
|
|
24
|
+
list(options: import("tinacms").MediaListOptions): Promise<import("tinacms").MediaList>;
|
|
25
|
+
parse: (img: any) => any;
|
|
26
|
+
buildQuery(options: import("tinacms").MediaListOptions): string;
|
|
27
|
+
};
|
|
28
|
+
};
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
|
|
3
|
+
*/
|
|
4
|
+
interface MediaListErrorConfig {
|
|
5
|
+
title: string;
|
|
6
|
+
message: string;
|
|
7
|
+
docsLink: string;
|
|
8
|
+
}
|
|
9
|
+
declare class MediaListError extends Error {
|
|
10
|
+
ERR_TYPE: string;
|
|
11
|
+
title: string;
|
|
12
|
+
docsLink: string;
|
|
13
|
+
constructor(config: MediaListErrorConfig);
|
|
14
|
+
}
|
|
15
|
+
export declare const E_DEFAULT: MediaListError;
|
|
16
|
+
export declare const E_UNAUTHORIZED: MediaListError;
|
|
17
|
+
export declare const E_CONFIG: MediaListError;
|
|
18
|
+
export declare const E_KEY_FAIL: MediaListError;
|
|
19
|
+
export declare const E_BAD_ROUTE: MediaListError;
|
|
20
|
+
export declare const interpretErrorMessage: (message: string) => MediaListError;
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
|
|
3
|
+
*/
|
|
4
|
+
import { NextApiRequest, NextApiResponse } from 'next';
|
|
5
|
+
export interface CloudinaryConfig {
|
|
6
|
+
cloud_name: string;
|
|
7
|
+
api_key: string;
|
|
8
|
+
api_secret: string;
|
|
9
|
+
authorized: (req: NextApiRequest, res: NextApiResponse) => Promise<boolean>;
|
|
10
|
+
}
|
|
11
|
+
export interface CloudinaryOptions {
|
|
12
|
+
useHttps?: boolean;
|
|
13
|
+
}
|
|
14
|
+
export declare const mediaHandlerConfig: {
|
|
15
|
+
api: {
|
|
16
|
+
bodyParser: boolean;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
export declare const createMediaHandler: (config: CloudinaryConfig, options?: CloudinaryOptions) => (req: NextApiRequest, res: NextApiResponse) => Promise<void>;
|
package/dist/handlers.js
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
|
|
29
|
+
// src/handlers.ts
|
|
30
|
+
var handlers_exports = {};
|
|
31
|
+
__export(handlers_exports, {
|
|
32
|
+
createMediaHandler: () => createMediaHandler,
|
|
33
|
+
mediaHandlerConfig: () => mediaHandlerConfig
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(handlers_exports);
|
|
36
|
+
var import_cloudinary = require("cloudinary");
|
|
37
|
+
var import_path = __toESM(require("path"));
|
|
38
|
+
var import_multer = __toESM(require("multer"));
|
|
39
|
+
var import_util = require("util");
|
|
40
|
+
var mediaHandlerConfig = {
|
|
41
|
+
api: {
|
|
42
|
+
bodyParser: false
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
var createMediaHandler = (config, options) => {
|
|
46
|
+
import_cloudinary.v2.config(Object.assign({ secure: true }, config));
|
|
47
|
+
return async (req, res) => {
|
|
48
|
+
const isAuthorized = await config.authorized(req, res);
|
|
49
|
+
if (!isAuthorized) {
|
|
50
|
+
res.status(401).json({ message: "sorry this user is unauthorized" });
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
switch (req.method) {
|
|
54
|
+
case "GET":
|
|
55
|
+
return listMedia(req, res, options);
|
|
56
|
+
case "POST":
|
|
57
|
+
return uploadMedia(req, res);
|
|
58
|
+
case "DELETE":
|
|
59
|
+
return deleteAsset(req, res);
|
|
60
|
+
default:
|
|
61
|
+
res.end(404);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
async function uploadMedia(req, res) {
|
|
66
|
+
const upload = (0, import_util.promisify)(
|
|
67
|
+
(0, import_multer.default)({
|
|
68
|
+
storage: import_multer.default.diskStorage({
|
|
69
|
+
// @ts-ignore
|
|
70
|
+
directory: (req2, file, cb) => {
|
|
71
|
+
cb(null, "/tmp");
|
|
72
|
+
},
|
|
73
|
+
filename: (req2, file, cb) => {
|
|
74
|
+
cb(null, file.originalname);
|
|
75
|
+
}
|
|
76
|
+
})
|
|
77
|
+
}).single("file")
|
|
78
|
+
);
|
|
79
|
+
await upload(req, res);
|
|
80
|
+
const { directory } = req.body;
|
|
81
|
+
try {
|
|
82
|
+
const result = await import_cloudinary.v2.uploader.upload(req.file.path, {
|
|
83
|
+
folder: directory.replace(/^\//, ""),
|
|
84
|
+
use_filename: true,
|
|
85
|
+
overwrite: false,
|
|
86
|
+
resource_type: "auto"
|
|
87
|
+
});
|
|
88
|
+
res.json(result);
|
|
89
|
+
} catch (error) {
|
|
90
|
+
res.status(error.http_code).json({ message: error.message });
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
async function listMedia(req, res, opts) {
|
|
94
|
+
try {
|
|
95
|
+
const mediaListOptions = {
|
|
96
|
+
directory: req.query.directory || '""',
|
|
97
|
+
limit: parseInt(req.query.limit, 10) || 500,
|
|
98
|
+
offset: req.query.offset,
|
|
99
|
+
filesOnly: req.query.filesOnly === "true" || false
|
|
100
|
+
};
|
|
101
|
+
const useRootDirectory = !mediaListOptions.directory || mediaListOptions.directory === "/" || mediaListOptions.directory === '""';
|
|
102
|
+
const query = useRootDirectory ? 'folder=""' : `folder="${mediaListOptions.directory}"`;
|
|
103
|
+
const response = await import_cloudinary.v2.search.expression(query).max_results(mediaListOptions.limit).next_cursor(mediaListOptions.offset).execute();
|
|
104
|
+
const files = response.resources.map(getCloudinaryToTinaFunc(opts));
|
|
105
|
+
import_cloudinary.v2.api.folders = (directory = '""') => {
|
|
106
|
+
if (useRootDirectory) {
|
|
107
|
+
return import_cloudinary.v2.api.root_folders();
|
|
108
|
+
} else {
|
|
109
|
+
return import_cloudinary.v2.api.sub_folders(directory);
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
let folders = [];
|
|
113
|
+
let folderRes = null;
|
|
114
|
+
if (mediaListOptions.filesOnly) {
|
|
115
|
+
res.json({
|
|
116
|
+
items: [...files],
|
|
117
|
+
offset: response.next_cursor
|
|
118
|
+
});
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
try {
|
|
122
|
+
folderRes = await import_cloudinary.v2.api.folders(mediaListOptions.directory);
|
|
123
|
+
} catch (e) {
|
|
124
|
+
if (e.error?.message.startsWith("Can't find folder with path")) {
|
|
125
|
+
} else {
|
|
126
|
+
console.error("Error getting folders");
|
|
127
|
+
console.error(e);
|
|
128
|
+
throw e;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (folderRes?.folders) {
|
|
132
|
+
folders = folderRes.folders.map(function(folder) {
|
|
133
|
+
"empty-repo/004";
|
|
134
|
+
return {
|
|
135
|
+
id: folder.path,
|
|
136
|
+
type: "dir",
|
|
137
|
+
filename: import_path.default.basename(folder.path),
|
|
138
|
+
directory: import_path.default.dirname(folder.path)
|
|
139
|
+
};
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
res.json({
|
|
143
|
+
items: [...folders, ...files],
|
|
144
|
+
offset: response.next_cursor
|
|
145
|
+
});
|
|
146
|
+
} catch (e) {
|
|
147
|
+
console.log(e);
|
|
148
|
+
res.status(500);
|
|
149
|
+
const message = findErrorMessage(e);
|
|
150
|
+
res.json({ e: message });
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
var findErrorMessage = (e) => {
|
|
154
|
+
if (typeof e == "string") return e;
|
|
155
|
+
if (e.message) return e.message;
|
|
156
|
+
if (e.error && e.error.message) return e.error.message;
|
|
157
|
+
return "an error occurred";
|
|
158
|
+
};
|
|
159
|
+
async function deleteAsset(req, res) {
|
|
160
|
+
const { media } = req.query;
|
|
161
|
+
const [, public_id] = media;
|
|
162
|
+
import_cloudinary.v2.uploader.destroy(public_id, {}, (err) => {
|
|
163
|
+
if (err) res.status(500);
|
|
164
|
+
res.json({
|
|
165
|
+
err,
|
|
166
|
+
public_id
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
function getCloudinaryToTinaFunc(opts) {
|
|
171
|
+
return function cloudinaryToTina(file) {
|
|
172
|
+
let useHttps = true;
|
|
173
|
+
if (typeof opts !== "undefined" && typeof opts.useHttps !== "undefined") {
|
|
174
|
+
useHttps = opts.useHttps;
|
|
175
|
+
}
|
|
176
|
+
const sel = useHttps ? "secure_url" : "url";
|
|
177
|
+
const filename = import_path.default.basename(file.public_id);
|
|
178
|
+
const directory = import_path.default.dirname(file.public_id);
|
|
179
|
+
return {
|
|
180
|
+
id: file.public_id,
|
|
181
|
+
filename,
|
|
182
|
+
directory,
|
|
183
|
+
src: file[sel],
|
|
184
|
+
thumbnails: {
|
|
185
|
+
"75x75": transformCloudinaryImage(file[sel], "w_75,h_75,c_fit,q_auto"),
|
|
186
|
+
"400x400": transformCloudinaryImage(
|
|
187
|
+
file[sel],
|
|
188
|
+
"w_400,h_400,c_fit,q_auto"
|
|
189
|
+
),
|
|
190
|
+
"1000x1000": transformCloudinaryImage(
|
|
191
|
+
file[sel],
|
|
192
|
+
"w_1000,h_1000,c_fit,q_auto"
|
|
193
|
+
)
|
|
194
|
+
},
|
|
195
|
+
type: "file"
|
|
196
|
+
};
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
function transformCloudinaryImage(url, transformations) {
|
|
200
|
+
const parts = url.split("/image/upload/");
|
|
201
|
+
if (parts.length === 2) {
|
|
202
|
+
return parts[0] + "/image/upload/" + transformations + "/" + parts[1];
|
|
203
|
+
}
|
|
204
|
+
return url;
|
|
205
|
+
}
|
|
206
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
207
|
+
0 && (module.exports = {
|
|
208
|
+
createMediaHandler,
|
|
209
|
+
mediaHandlerConfig
|
|
210
|
+
});
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
(function(global, factory) {
|
|
2
|
+
typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("tinacms")) : typeof define === "function" && define.amd ? define(["exports", "tinacms"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global["next-tinacms-cloudinary"] = {}, global.NOOP));
|
|
3
|
+
})(this, function(exports2, tinacms) {
|
|
4
|
+
"use strict";
|
|
5
|
+
class MediaListError extends Error {
|
|
6
|
+
constructor(config) {
|
|
7
|
+
super(config.message);
|
|
8
|
+
this.ERR_TYPE = "MediaListError";
|
|
9
|
+
this.title = config.title;
|
|
10
|
+
this.docsLink = config.docsLink;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
const E_DEFAULT = new MediaListError({
|
|
14
|
+
title: "An Error Occurred",
|
|
15
|
+
message: "Something went wrong fetching your media from Cloudinary.",
|
|
16
|
+
docsLink: "https://tina.io/docs/r/cloudinary"
|
|
17
|
+
});
|
|
18
|
+
const E_UNAUTHORIZED = new MediaListError({
|
|
19
|
+
title: "Unauthorized",
|
|
20
|
+
message: "You don't have access to this resource.",
|
|
21
|
+
docsLink: "https://tina.io/docs/r/cloudinary"
|
|
22
|
+
});
|
|
23
|
+
const E_CONFIG = new MediaListError({
|
|
24
|
+
title: "Missing Credentials",
|
|
25
|
+
message: "Unable to connect to Cloudinary because one or more environment variables are missing.",
|
|
26
|
+
docsLink: "https://tina.io/docs/r/cloudinary/"
|
|
27
|
+
});
|
|
28
|
+
const E_KEY_FAIL = new MediaListError({
|
|
29
|
+
title: "Bad Credentials",
|
|
30
|
+
message: "Unable to connect to Cloudinary because one or more environment variables are misconfigured.",
|
|
31
|
+
docsLink: "https://tina.io/docs/r/cloudinary/"
|
|
32
|
+
});
|
|
33
|
+
const E_BAD_ROUTE = new MediaListError({
|
|
34
|
+
title: "Bad Route",
|
|
35
|
+
message: "The Cloudinary API route is missing or misconfigured.",
|
|
36
|
+
docsLink: "https://tina.io/docs/r/cloudinary"
|
|
37
|
+
});
|
|
38
|
+
const interpretErrorMessage = (message) => {
|
|
39
|
+
switch (message) {
|
|
40
|
+
case "Must supply cloud_name":
|
|
41
|
+
case "Must supply api_key":
|
|
42
|
+
case "Must supply api_secret":
|
|
43
|
+
return E_CONFIG;
|
|
44
|
+
case "unknown api_key":
|
|
45
|
+
return E_KEY_FAIL;
|
|
46
|
+
default:
|
|
47
|
+
return E_DEFAULT;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
class CloudinaryMediaStore {
|
|
51
|
+
constructor(options) {
|
|
52
|
+
this.fetchFunction = (input, init) => fetch(input, init);
|
|
53
|
+
this.accept = tinacms.DEFAULT_MEDIA_UPLOAD_TYPES;
|
|
54
|
+
this.parse = (img) => {
|
|
55
|
+
return img.src;
|
|
56
|
+
};
|
|
57
|
+
this.baseUrl = (options == null ? void 0 : options.baseUrl) || "/api/cloudinary/media";
|
|
58
|
+
}
|
|
59
|
+
async persist(media) {
|
|
60
|
+
const newFiles = [];
|
|
61
|
+
for (const item of media) {
|
|
62
|
+
const { file, directory } = item;
|
|
63
|
+
const formData = new FormData();
|
|
64
|
+
formData.append("file", file);
|
|
65
|
+
formData.append("directory", directory);
|
|
66
|
+
formData.append("filename", file.name);
|
|
67
|
+
const res = await this.fetchFunction(this.baseUrl, {
|
|
68
|
+
method: "POST",
|
|
69
|
+
body: formData
|
|
70
|
+
});
|
|
71
|
+
if (res.status != 200) {
|
|
72
|
+
const responseData = await res.json();
|
|
73
|
+
throw new Error(responseData.message);
|
|
74
|
+
}
|
|
75
|
+
const fileRes = await res.json();
|
|
76
|
+
await new Promise((resolve) => {
|
|
77
|
+
setTimeout(resolve, 2e3);
|
|
78
|
+
});
|
|
79
|
+
const parsedRes = {
|
|
80
|
+
type: "file",
|
|
81
|
+
id: fileRes.public_id,
|
|
82
|
+
filename: fileRes.original_filename,
|
|
83
|
+
directory: "/",
|
|
84
|
+
thumbnails: {
|
|
85
|
+
"75x75": fileRes.secure_url,
|
|
86
|
+
"400x400": fileRes.secure_url,
|
|
87
|
+
"1000x1000": fileRes.secure_url
|
|
88
|
+
},
|
|
89
|
+
src: fileRes.secure_url
|
|
90
|
+
};
|
|
91
|
+
newFiles.push(parsedRes);
|
|
92
|
+
}
|
|
93
|
+
return newFiles;
|
|
94
|
+
}
|
|
95
|
+
async delete(media) {
|
|
96
|
+
await this.fetchFunction(
|
|
97
|
+
`${this.baseUrl}/${encodeURIComponent(media.id)}`,
|
|
98
|
+
{
|
|
99
|
+
method: "DELETE"
|
|
100
|
+
}
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
async list(options) {
|
|
104
|
+
const query = this.buildQuery(options);
|
|
105
|
+
const response = await this.fetchFunction(this.baseUrl + query);
|
|
106
|
+
if (response.status == 401) {
|
|
107
|
+
throw E_UNAUTHORIZED;
|
|
108
|
+
}
|
|
109
|
+
if (response.status == 404) {
|
|
110
|
+
throw E_BAD_ROUTE;
|
|
111
|
+
}
|
|
112
|
+
if (response.status >= 500) {
|
|
113
|
+
const { e } = await response.json();
|
|
114
|
+
const error = interpretErrorMessage(e);
|
|
115
|
+
throw error;
|
|
116
|
+
}
|
|
117
|
+
const { items, offset } = await response.json();
|
|
118
|
+
return {
|
|
119
|
+
items: items.map((item) => item),
|
|
120
|
+
nextOffset: offset
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
buildQuery(options) {
|
|
124
|
+
const params = Object.keys(options).filter((key) => options[key] !== "" && options[key] !== void 0).map((key) => `${key}=${options[key]}`).join("&");
|
|
125
|
+
return `?${params}`;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
const createTinaCloudCloudinaryMediaStore = (options = { baseUrl: "/api/cloudinary/media" }) => class TinaCloudCloudinaryMediaStore extends CloudinaryMediaStore {
|
|
129
|
+
constructor(client) {
|
|
130
|
+
super(options);
|
|
131
|
+
this.client = client;
|
|
132
|
+
this.fetchFunction = async (input, init) => {
|
|
133
|
+
try {
|
|
134
|
+
const url = input.toString();
|
|
135
|
+
const query = `${url.includes("?") ? "&" : "?"}clientID=${client.clientId}`;
|
|
136
|
+
const res = client.authProvider.fetchWithToken(url + query, init);
|
|
137
|
+
return res;
|
|
138
|
+
} catch (error) {
|
|
139
|
+
console.error(error);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
const TinaCloudCloudinaryMediaStore = createTinaCloudCloudinaryMediaStore();
|
|
145
|
+
exports2.CloudinaryMediaStore = CloudinaryMediaStore;
|
|
146
|
+
exports2.TinaCloudCloudinaryMediaStore = TinaCloudCloudinaryMediaStore;
|
|
147
|
+
exports2.createTinaCloudCloudinaryMediaStore = createTinaCloudCloudinaryMediaStore;
|
|
148
|
+
Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
|
|
149
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
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 Cloudinary.",
|
|
13
|
+
docsLink: "https://tina.io/docs/r/cloudinary"
|
|
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/docs/r/cloudinary"
|
|
19
|
+
});
|
|
20
|
+
const E_CONFIG = new MediaListError({
|
|
21
|
+
title: "Missing Credentials",
|
|
22
|
+
message: "Unable to connect to Cloudinary because one or more environment variables are missing.",
|
|
23
|
+
docsLink: "https://tina.io/docs/r/cloudinary/"
|
|
24
|
+
});
|
|
25
|
+
const E_KEY_FAIL = new MediaListError({
|
|
26
|
+
title: "Bad Credentials",
|
|
27
|
+
message: "Unable to connect to Cloudinary because one or more environment variables are misconfigured.",
|
|
28
|
+
docsLink: "https://tina.io/docs/r/cloudinary/"
|
|
29
|
+
});
|
|
30
|
+
const E_BAD_ROUTE = new MediaListError({
|
|
31
|
+
title: "Bad Route",
|
|
32
|
+
message: "The Cloudinary API route is missing or misconfigured.",
|
|
33
|
+
docsLink: "https://tina.io/docs/r/cloudinary"
|
|
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 CloudinaryMediaStore {
|
|
48
|
+
constructor(options) {
|
|
49
|
+
this.fetchFunction = (input, init) => fetch(input, init);
|
|
50
|
+
this.accept = DEFAULT_MEDIA_UPLOAD_TYPES;
|
|
51
|
+
this.parse = (img) => {
|
|
52
|
+
return img.src;
|
|
53
|
+
};
|
|
54
|
+
this.baseUrl = (options == null ? void 0 : options.baseUrl) || "/api/cloudinary/media";
|
|
55
|
+
}
|
|
56
|
+
async persist(media) {
|
|
57
|
+
const newFiles = [];
|
|
58
|
+
for (const item of media) {
|
|
59
|
+
const { file, directory } = item;
|
|
60
|
+
const formData = new FormData();
|
|
61
|
+
formData.append("file", file);
|
|
62
|
+
formData.append("directory", directory);
|
|
63
|
+
formData.append("filename", file.name);
|
|
64
|
+
const res = await this.fetchFunction(this.baseUrl, {
|
|
65
|
+
method: "POST",
|
|
66
|
+
body: formData
|
|
67
|
+
});
|
|
68
|
+
if (res.status != 200) {
|
|
69
|
+
const responseData = await res.json();
|
|
70
|
+
throw new Error(responseData.message);
|
|
71
|
+
}
|
|
72
|
+
const fileRes = await res.json();
|
|
73
|
+
await new Promise((resolve) => {
|
|
74
|
+
setTimeout(resolve, 2e3);
|
|
75
|
+
});
|
|
76
|
+
const parsedRes = {
|
|
77
|
+
type: "file",
|
|
78
|
+
id: fileRes.public_id,
|
|
79
|
+
filename: fileRes.original_filename,
|
|
80
|
+
directory: "/",
|
|
81
|
+
thumbnails: {
|
|
82
|
+
"75x75": fileRes.secure_url,
|
|
83
|
+
"400x400": fileRes.secure_url,
|
|
84
|
+
"1000x1000": fileRes.secure_url
|
|
85
|
+
},
|
|
86
|
+
src: fileRes.secure_url
|
|
87
|
+
};
|
|
88
|
+
newFiles.push(parsedRes);
|
|
89
|
+
}
|
|
90
|
+
return newFiles;
|
|
91
|
+
}
|
|
92
|
+
async delete(media) {
|
|
93
|
+
await this.fetchFunction(
|
|
94
|
+
`${this.baseUrl}/${encodeURIComponent(media.id)}`,
|
|
95
|
+
{
|
|
96
|
+
method: "DELETE"
|
|
97
|
+
}
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
async list(options) {
|
|
101
|
+
const query = this.buildQuery(options);
|
|
102
|
+
const response = await this.fetchFunction(this.baseUrl + query);
|
|
103
|
+
if (response.status == 401) {
|
|
104
|
+
throw E_UNAUTHORIZED;
|
|
105
|
+
}
|
|
106
|
+
if (response.status == 404) {
|
|
107
|
+
throw E_BAD_ROUTE;
|
|
108
|
+
}
|
|
109
|
+
if (response.status >= 500) {
|
|
110
|
+
const { e } = await response.json();
|
|
111
|
+
const error = interpretErrorMessage(e);
|
|
112
|
+
throw error;
|
|
113
|
+
}
|
|
114
|
+
const { items, offset } = await response.json();
|
|
115
|
+
return {
|
|
116
|
+
items: items.map((item) => item),
|
|
117
|
+
nextOffset: offset
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
buildQuery(options) {
|
|
121
|
+
const params = Object.keys(options).filter((key) => options[key] !== "" && options[key] !== void 0).map((key) => `${key}=${options[key]}`).join("&");
|
|
122
|
+
return `?${params}`;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
const createTinaCloudCloudinaryMediaStore = (options = { baseUrl: "/api/cloudinary/media" }) => class TinaCloudCloudinaryMediaStore extends CloudinaryMediaStore {
|
|
126
|
+
constructor(client) {
|
|
127
|
+
super(options);
|
|
128
|
+
this.client = client;
|
|
129
|
+
this.fetchFunction = async (input, init) => {
|
|
130
|
+
try {
|
|
131
|
+
const url = input.toString();
|
|
132
|
+
const query = `${url.includes("?") ? "&" : "?"}clientID=${client.clientId}`;
|
|
133
|
+
const res = client.authProvider.fetchWithToken(url + query, init);
|
|
134
|
+
return res;
|
|
135
|
+
} catch (error) {
|
|
136
|
+
console.error(error);
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
const TinaCloudCloudinaryMediaStore = createTinaCloudCloudinaryMediaStore();
|
|
142
|
+
export {
|
|
143
|
+
CloudinaryMediaStore,
|
|
144
|
+
TinaCloudCloudinaryMediaStore,
|
|
145
|
+
createTinaCloudCloudinaryMediaStore
|
|
146
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "next-tinacms-cloudinary",
|
|
3
|
+
"version": "0.0.0-003e348-20251023020516",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"module": "dist/index.mjs",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"typings": "dist/index.d.ts",
|
|
10
|
+
"license": "Apache-2.0",
|
|
11
|
+
"buildConfig": {
|
|
12
|
+
"entryPoints": [
|
|
13
|
+
"src/index.ts",
|
|
14
|
+
{
|
|
15
|
+
"name": "src/handlers.ts",
|
|
16
|
+
"target": "node"
|
|
17
|
+
}
|
|
18
|
+
]
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"cloudinary": "^1.41.3",
|
|
22
|
+
"multer": "1.4.5-lts.1"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/crypto-js": "^3.1.47",
|
|
26
|
+
"@types/js-cookie": "^2.2.7",
|
|
27
|
+
"@types/node": "^22.13.1",
|
|
28
|
+
"next": "14.2.10",
|
|
29
|
+
"react": "^18.3.1",
|
|
30
|
+
"react-dom": "^18.3.1",
|
|
31
|
+
"typescript": "^5.7.3",
|
|
32
|
+
"@tinacms/scripts": "1.4.0",
|
|
33
|
+
"tinacms": "0.0.0-003e348-20251023020516"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"tinacms": "0.0.0-003e348-20251023020516"
|
|
37
|
+
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"registry": "https://registry.npmjs.org"
|
|
40
|
+
},
|
|
41
|
+
"repository": {
|
|
42
|
+
"url": "https://github.com/tinacms/tinacms.git",
|
|
43
|
+
"directory": "packages/next-tinacms-cloudinary"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"types": "pnpm tsc",
|
|
47
|
+
"build": "tinacms-scripts build"
|
|
48
|
+
}
|
|
49
|
+
}
|