• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Mintbase / mintbase-js / 4015446410

pending completion
4015446410

push

github

Sérgio
📝 Fixed main docs

483 of 648 branches covered (74.54%)

Branch coverage included in aggregate %.

721 of 780 relevant lines covered (92.44%)

4.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

63.89
/packages/storage/src/uploads.ts
1
import { ANON_USER_WARNING, ARWEAVE_SERVICE_HOST, MAX_UPLOAD_ERROR_MSG, MINTBASE_API_ANON_USER, MINTBASE_API_KEY, MINTBASE_API_KEY_HEADER } from './constants';
3✔
2
import superagent from 'superagent';
3✔
3

4
export const MAX_UPLOAD_BYTES = 31_457_280;
3✔
5
export const OBJECT_IS_EMPTY_ERROR = 'Provided object is empty';
3✔
6
export type ArweaveResponse = {
7
  id: string;
8
  block: string;
9
  name: string;
10
  mimeType: string;
11
};
12

13
type HttpError = {
14
  status: number;
15
  response: Response;
16
};
17

18
type ReferenceObject = any & {
19
  title?: string;
20
  description?: string;
21
  media?: File;
22
  animation_url?: File;
23
  document?: File;
24
  attributes?: Trait[];
25
  category?: string;
26
  tags?: string[];
27
  extra?: Trait[] | any;
28
}
29

30
type Trait = {
31
  display_type: string;
32
  trait_type: string;
33
  value: number;
34
}
35

36

37
/**
38
 * (NodeJS) upload a file via POST to upload service
39
 * @param file A file to upload
40
 * @param name The name of the file to upload
41
 */
42
export const uploadBuffer = async (
3✔
43
  file: Buffer,
44
  name: string,
45
): Promise<ArweaveResponse> => {
3✔
46
  if (MINTBASE_API_KEY == MINTBASE_API_ANON_USER) {
3!
47
    console.warn(ANON_USER_WARNING);
×
48
  }
49

50
  const size = (file as Buffer).length;
3✔
51

52
  // if size is more than 30MB, throw since cloud run won't upload.
53
  if (size > MAX_UPLOAD_BYTES) {
3✔
54
    throw new Error(MAX_UPLOAD_ERROR_MSG);
1✔
55
  }
56

57
  try {
2✔
58
    const { body } = await superagent
2✔
59
      .post(ARWEAVE_SERVICE_HOST)
60
      .set({
61
        [MINTBASE_API_KEY_HEADER]: MINTBASE_API_KEY,
62
      })
63
      .attach('file', file, name);
64
    return body;
1✔
65
  } catch (err: unknown) {
66
    const httpError = err as HttpError;
1✔
67
    console.error(
1✔
68
      `Uploading file to arweave failed: ${httpError.status} ${httpError.response.text}`,
69
    );
70
    throw err;
×
71
  }
72
};
73

74
/**
75
 * (Browser) upload a file via POST to upload service
76
 * @param file A file to upload
77
 */
78
export const uploadFile = async (
3✔
79
  file: File,
80
): Promise<ArweaveResponse> => {
2✔
81

82
  if (MINTBASE_API_KEY == MINTBASE_API_ANON_USER) {
2!
83
    console.warn(ANON_USER_WARNING);
×
84
  }
85

86

87
  if (file.size > MAX_UPLOAD_BYTES) {
2✔
88
    throw new Error(MAX_UPLOAD_ERROR_MSG);
1✔
89
  }
90

91
  const formdata = new FormData();
1✔
92
  formdata.append('file', file, 'file');
1✔
93

94
  try {
1✔
95
    const request = await fetch(ARWEAVE_SERVICE_HOST, {
1✔
96
      method: 'POST',
97
      headers: {
98
        'mb-api-key': MINTBASE_API_KEY,
99
      },
100
      body: formdata,
101
      redirect: 'follow',
102
    });
103

104
    if (request.status !== 200) {
1!
105
      throw new Error(
×
106
        `Error uploading via arweave service: ${await request.json()}`,
107
      );
108
    }
109

110
    const result = (await request.json()) as {
1✔
111
      id: string;
112
      block: string;
113
      name: string;
114
      mimeType: string;
115
    };
116

117
    return result;
1✔
118
  } catch (error: unknown) {
119
    console.error('Uploading file to arweave failed');
×
120
    throw error;
×
121
  }
122
};
123

124
/**
125
 * (Browser) upload a json reference object via POST to upload service
126
 * @param ReferenceObject A json reference object to upload
127
 */
128
export const uploadReference = async (
3✔
129
  referenceObject: ReferenceObject,
130
): Promise<ArweaveResponse> => {
3✔
131

132
  if (Object.keys(referenceObject).length == 0) {
3!
133
    throw new Error(OBJECT_IS_EMPTY_ERROR);
×
134
  }
135
  const { media, animation_url, document } = referenceObject;
3✔
136
  const formdata = new FormData();
3✔
137

138
  Object.entries(referenceObject).forEach((key: any, value: any): void => {
3✔
139
    if (!(key == (media || animation_url || document))) {
8!
140
      formdata.append(key, value);
8✔
141
    } else if (value?.size < MAX_UPLOAD_BYTES) {
×
142
      formdata.append(key, value);
×
143
    }
144

145
  });
146

147
  try {
3✔
148
    const request = await fetch(`${ARWEAVE_SERVICE_HOST}/reference`, {
3✔
149
      method: 'POST',
150
      headers: {
151
        'mb-api-key': MINTBASE_API_KEY,
152
      },
153
      body: formdata,
154
      redirect: 'follow',
155
    });
156

157
    if (request.status !== 200) {
3!
158
      throw new Error(
×
159
        `Error uploading via arweave service: ${await request.json()}`,
160
      );
161
    }
162

163
    const result = (await request.json()) as {
3✔
164
      id: string;
165
      block: string;
166
      name: string;
167
      mimeType: string;
168
    };
169

170
    return result;
3✔
171
  } catch (error: unknown) {
172
    console.error('Uploading file to arweave failed');
×
173
    throw error;
×
174
  }
175
};
176

177
export function getFileFromObject(referenceObject: unknown): File {
3✔
178
  const str = JSON.stringify(referenceObject);
×
179
  return new File([str], 'file', {
×
180
    type: 'application/json;charset=utf-8',
181
  });
182
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc