-
Notifications
You must be signed in to change notification settings - Fork 167
/
db-transforms.js
102 lines (97 loc) · 2.64 KB
/
db-transforms.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import * as cluster from '../cluster.js'
/**
* We mixed upload type and content type. This was split into `type` and
* `mime_type` in the move to Postgres but the API was not changed. This object
* maps the DB `type`s that we want to use in the API response `type`. Other
* API response `type`s are DB `mime_type`.
* @type {Record<string, string>}
*/
const typeMap = {
Nft: 'nft',
Multipart: 'directory',
Remote: 'remote',
}
/**
* Transform db response into NFT response
*
* @param {import('./db-client-types').UploadOutput} upload
* @param {string} [sourceCid] - User input CID so we can return the same cid version back
*/
export function toNFTResponse(upload, sourceCid) {
/** @type {import('../bindings').NFTResponse} */
const nft = {
cid: sourceCid || upload.source_cid,
created: upload.inserted_at,
type: typeMap[upload.type] || upload.mime_type || '',
scope: upload.key ? upload.key.name : 'session',
files: upload.files,
size: upload.content.dag_size || 0,
name: upload.name,
pin: {
cid: sourceCid || upload.source_cid,
created: upload.content.pin[0].inserted_at,
size: upload.content.dag_size || 0,
status: transformPinStatus(upload.content.pin[0].status),
},
deals: upload.deals,
}
return nft
}
/**
* Transform db response into Pin response
*
* @param {import('./db-client-types').UploadOutput} upload
*/
export function toPinsResponse(upload) {
/** @type {import('../bindings').PinsResponse} */
const rsp = {
requestid: upload.source_cid,
status: transformPinStatus(upload.content.pin[0].status),
created: upload.inserted_at,
pin: {
cid: upload.source_cid,
meta: upload.meta,
name: upload.name,
origins: upload.origins,
},
delegates: cluster.delegates(),
}
return rsp
}
/**
* Transform db response into Check nft response
*
* @param {string} sourceCid
* @param {import('./db-client-types').ContentOutput} content
*/
export function toCheckNftResponse(sourceCid, content) {
/** @type {import('../bindings').CheckNFTResponse} */
const rsp = {
cid: sourceCid,
pin: {
cid: sourceCid,
created: content?.pins[0].inserted_at,
size: content?.dag_size,
status: transformPinStatus(content?.pins[0].status),
},
deals: content.deals,
}
return rsp
}
/**
*
* @param {import('./db-types').definitions['pin']['status']} status
* @return {import('../bindings').PinStatus}
*/
function transformPinStatus(status) {
switch (status) {
case 'PinQueued':
return 'queued'
case 'Pinned':
return 'pinned'
case 'Pinning':
return 'pinning'
default:
return 'failed'
}
}