|
1 | 1 | import { randomBytes } from 'node:crypto' |
2 | 2 | import type { CommonFieldConfig, BaseListTypeInfo, FieldTypeFunc } from '@keystone-6/core/types' |
3 | | -import { jsonFieldTypePolyfilledForSQLite } from '@keystone-6/core/types' |
| 3 | +import { fieldType } from '@keystone-6/core/types' |
4 | 4 | import { g } from '@keystone-6/core' |
5 | 5 | import type Cloudinary from 'cloudinary' |
6 | 6 | import { v2 as cloudinary } from 'cloudinary' |
@@ -160,62 +160,61 @@ export function cloudinaryImage<ListTypeInfo extends BaseListTypeInfo>({ |
160 | 160 | } |
161 | 161 | } |
162 | 162 |
|
163 | | - return jsonFieldTypePolyfilledForSQLite( |
164 | | - meta.provider, |
165 | | - { |
166 | | - ...config, |
167 | | - __ksTelemetryFieldTypeName: '@keystone-6/cloudinary', |
168 | | - input: { |
169 | | - create: { arg: inputArg, resolve: resolveInput }, |
170 | | - update: { arg: inputArg, resolve: resolveInput }, |
171 | | - }, |
172 | | - output: g.field({ |
173 | | - type: outputType, |
174 | | - resolve({ value }) { |
175 | | - if (value === null) return null |
176 | | - const val = value as any |
177 | | - return { |
178 | | - width: val?._meta.width, |
179 | | - height: val?._meta.width, |
180 | | - filesize: val?._meta.bytes, |
181 | | - publicUrl: val?._meta?.secure_url ?? null, |
182 | | - publicUrlTransformed: ({ |
183 | | - transformation, |
184 | | - }: { |
185 | | - transformation: InferValueFromArg<GArg<typeof CloudinaryImageFormat>> |
186 | | - }) => { |
187 | | - if (!val._meta) return null |
188 | | - |
189 | | - const { prettyName, ...rest } = transformation ?? {} |
190 | | - |
191 | | - // no formatting options provided, return the publicUrl field |
192 | | - if (!Object.keys(rest).length) return val?._meta?.secure_url ?? null |
193 | | - |
194 | | - const { public_id, format } = val._meta |
195 | | - |
196 | | - // ref https://github.com/cloudinary/cloudinary_npm/blob/439586eac73cee7f2803cf19f885e98f237183b3/src/utils.coffee#L472 |
197 | | - return cloudinary.url(public_id, { |
198 | | - type: 'upload', |
199 | | - format, |
200 | | - secure: true, // the default as of version 2 |
201 | | - url_suffix: prettyName, |
202 | | - transformation, |
203 | | - cloud_name: cloudinaryConfig.cloudName, |
204 | | - |
205 | | - // SDK analytics defaults to true in version 2 (ref https://github.com/cloudinary/cloudinary_npm/commit/d2510eb677e553a45bc7e363b35d2c20b4c4b144#diff-9aa82f0ed674e050695a7422b1cd56d43ce47e6953688a16a003bf49c3481622) |
206 | | - // we default to false for the least surprise, keeping this upgrade as a patch |
207 | | - urlAnalytics: false, |
208 | | - }) |
209 | | - }, |
210 | | - ...val, |
211 | | - } |
212 | | - }, |
213 | | - }), |
214 | | - views: '@keystone-6/cloudinary/views', |
| 163 | + return fieldType({ |
| 164 | + kind: 'scalar', |
| 165 | + mode: 'optional', |
| 166 | + scalar: 'Json', |
| 167 | + map: config.db?.map, |
| 168 | + })({ |
| 169 | + ...config, |
| 170 | + __ksTelemetryFieldTypeName: '@keystone-6/cloudinary', |
| 171 | + input: { |
| 172 | + create: { arg: inputArg, resolve: resolveInput }, |
| 173 | + update: { arg: inputArg, resolve: resolveInput }, |
215 | 174 | }, |
216 | | - { |
217 | | - map: config.db?.map, |
218 | | - } |
219 | | - ) |
| 175 | + output: g.field({ |
| 176 | + type: outputType, |
| 177 | + resolve({ value }) { |
| 178 | + if (value === null) return null |
| 179 | + const val = value as any |
| 180 | + return { |
| 181 | + width: val?._meta.width, |
| 182 | + height: val?._meta.width, |
| 183 | + filesize: val?._meta.bytes, |
| 184 | + publicUrl: val?._meta?.secure_url ?? null, |
| 185 | + publicUrlTransformed: ({ |
| 186 | + transformation, |
| 187 | + }: { |
| 188 | + transformation: InferValueFromArg<GArg<typeof CloudinaryImageFormat>> |
| 189 | + }) => { |
| 190 | + if (!val._meta) return null |
| 191 | + |
| 192 | + const { prettyName, ...rest } = transformation ?? {} |
| 193 | + |
| 194 | + // no formatting options provided, return the publicUrl field |
| 195 | + if (!Object.keys(rest).length) return val?._meta?.secure_url ?? null |
| 196 | + |
| 197 | + const { public_id, format } = val._meta |
| 198 | + |
| 199 | + // ref https://github.com/cloudinary/cloudinary_npm/blob/439586eac73cee7f2803cf19f885e98f237183b3/src/utils.coffee#L472 |
| 200 | + return cloudinary.url(public_id, { |
| 201 | + type: 'upload', |
| 202 | + format, |
| 203 | + secure: true, // the default as of version 2 |
| 204 | + url_suffix: prettyName, |
| 205 | + transformation, |
| 206 | + cloud_name: cloudinaryConfig.cloudName, |
| 207 | + |
| 208 | + // SDK analytics defaults to true in version 2 (ref https://github.com/cloudinary/cloudinary_npm/commit/d2510eb677e553a45bc7e363b35d2c20b4c4b144#diff-9aa82f0ed674e050695a7422b1cd56d43ce47e6953688a16a003bf49c3481622) |
| 209 | + // we default to false for the least surprise, keeping this upgrade as a patch |
| 210 | + urlAnalytics: false, |
| 211 | + }) |
| 212 | + }, |
| 213 | + ...val, |
| 214 | + } |
| 215 | + }, |
| 216 | + }), |
| 217 | + views: '@keystone-6/cloudinary/views', |
| 218 | + }) |
220 | 219 | } |
221 | 220 | } |
0 commit comments