Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit c339a15

Browse files
authored
Update prettier configuration to include TypeScript files (#163)
1 parent ce179ae commit c339a15

12 files changed

+428
-376
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"_build": "yarn build:node && yarn build:browser",
4545
"_babel": "babel --extensions '.ts' --out-dir lib/ src/",
4646
"_nyc": "nyc --nycrc-path .nycrc",
47-
"_prettier": "prettier --config prettier.config.js --ignore-path .prettierignore --write './**/*.js'"
47+
"_prettier": "prettier --config prettier.config.js --ignore-path .prettierignore --write './**/*.{js,ts}'"
4848
},
4949
"husky": {
5050
"hooks": {

prettier.config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ module.exports = {
99
tabWidth: 2,
1010
parser: 'babel',
1111
trailingComma: 'none',
12-
useTabs: false
12+
useTabs: false,
13+
parser: 'typescript'
1314
};

src/axios.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import axios from "axios";
1+
import axios from 'axios';
22

33
/* tslint:disable-next-line:no-var-requires */
4-
export const version = require("../package.json").version;
4+
export const version = require('../package.json').version;
55
export const AxiosClient = axios.create({
66
headers: {
7-
"X-Client-Name": "js-soroban-client",
8-
"X-Client-Version": version,
9-
},
7+
'X-Client-Name': 'js-soroban-client',
8+
'X-Client-Version': version
9+
}
1010
});
1111

12-
export default AxiosClient;
12+
export default AxiosClient;

src/browser.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* tslint:disable:no-var-requires */
22

3-
export * from "./index";
4-
export * as StellarBase from "stellar-base";
3+
export * from './index';
4+
export * as StellarBase from 'stellar-base';
55

6-
import axios from "axios"; // idk why axios is weird
6+
import axios from 'axios'; // idk why axios is weird
77
export { axios };
88

99
export default module.exports;

src/contract_spec.ts

+39-35
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { ScIntType, XdrLargeInt, xdr } from "stellar-base";
1+
import { ScIntType, XdrLargeInt, xdr } from 'stellar-base';
22

3-
import { Address } from "stellar-base";
4-
import { Contract } from "stellar-base";
5-
import { scValToBigInt } from "stellar-base";
3+
import { Address } from 'stellar-base';
4+
import { Contract } from 'stellar-base';
5+
import { scValToBigInt } from 'stellar-base';
66

77
export interface Union<T> {
88
tag: string;
@@ -44,12 +44,12 @@ export class ContractSpec {
4444
*/
4545
constructor(entries: xdr.ScSpecEntry[] | string[]) {
4646
if (entries.length == 0) {
47-
throw new Error("Contract spec must have at least one entry");
47+
throw new Error('Contract spec must have at least one entry');
4848
}
4949
let entry = entries[0];
50-
if (typeof entry === "string") {
50+
if (typeof entry === 'string') {
5151
this.entries = (entries as string[]).map((s) =>
52-
xdr.ScSpecEntry.fromXDR(s, "base64")
52+
xdr.ScSpecEntry.fromXDR(s, 'base64')
5353
);
5454
} else {
5555
this.entries = entries as xdr.ScSpecEntry[];
@@ -115,8 +115,8 @@ export class ContractSpec {
115115
*/
116116
funcResToNative(name: string, val_or_base64: xdr.ScVal | string): any {
117117
let val =
118-
typeof val_or_base64 === "string"
119-
? xdr.ScVal.fromXDR(val_or_base64, "base64")
118+
typeof val_or_base64 === 'string'
119+
? xdr.ScVal.fromXDR(val_or_base64, 'base64')
120120
: val_or_base64;
121121
let func = this.getFunc(name);
122122
let outputs = func.outputs();
@@ -132,10 +132,7 @@ export class ContractSpec {
132132
}
133133
let output = outputs[0];
134134
if (output.switch().value === xdr.ScSpecType.scSpecTypeResult().value) {
135-
return this.scValToNative(
136-
val,
137-
output.result().okType()
138-
);
135+
return this.scValToNative(val, output.result().okType());
139136
}
140137
return this.scValToNative(val, output);
141138
}
@@ -183,7 +180,7 @@ export class ContractSpec {
183180
}
184181

185182
switch (typeof val) {
186-
case "object": {
183+
case 'object': {
187184
if (val === null) {
188185
switch (value) {
189186
case xdr.ScSpecType.scSpecTypeVoid().value:
@@ -280,11 +277,10 @@ export class ContractSpec {
280277
return xdr.ScVal.scvMap(entries);
281278
}
282279

283-
if ((val.constructor?.name ?? "") !== "Object") {
280+
if ((val.constructor?.name ?? '') !== 'Object') {
284281
throw new TypeError(
285-
`cannot interpret ${
286-
val.constructor?.name
287-
} value as ScVal (${JSON.stringify(val)})`
282+
`cannot interpret ${val.constructor
283+
?.name} value as ScVal (${JSON.stringify(val)})`
288284
);
289285
}
290286

@@ -293,8 +289,8 @@ export class ContractSpec {
293289
);
294290
}
295291

296-
case "number":
297-
case "bigint": {
292+
case 'number':
293+
case 'bigint': {
298294
switch (value) {
299295
case xdr.ScSpecType.scSpecTypeU32().value:
300296
return xdr.ScVal.scvU32(val as number);
@@ -313,16 +309,16 @@ export class ContractSpec {
313309
throw new TypeError(`invalid type (${ty}) specified for integer`);
314310
}
315311
}
316-
case "string":
312+
case 'string':
317313
return stringToScVal(val, t);
318314

319-
case "boolean": {
315+
case 'boolean': {
320316
if (value !== xdr.ScSpecType.scSpecTypeBool().value) {
321317
throw TypeError(`Type ${ty} was not bool, but value was bool`);
322318
}
323319
return xdr.ScVal.scvBool(val);
324320
}
325-
case "undefined": {
321+
case 'undefined': {
326322
if (!ty) {
327323
return xdr.ScVal.scvVoid();
328324
}
@@ -337,7 +333,7 @@ export class ContractSpec {
337333
}
338334
}
339335

340-
case "function": // FIXME: Is this too helpful?
336+
case 'function': // FIXME: Is this too helpful?
341337
return this.nativeToScVal(val(), ty);
342338

343339
default:
@@ -349,7 +345,7 @@ export class ContractSpec {
349345
let entry = this.findEntry(name);
350346
switch (entry.switch()) {
351347
case xdr.ScSpecEntryKind.scSpecEntryUdtEnumV0():
352-
if (typeof val !== "number") {
348+
if (typeof val !== 'number') {
353349
throw new TypeError(
354350
`expected number for enum ${name}, but got ${typeof val}`
355351
);
@@ -418,7 +414,7 @@ export class ContractSpec {
418414
if (fields.some(isNumeric)) {
419415
if (!fields.every(isNumeric)) {
420416
throw new Error(
421-
"mixed numeric and non-numeric field names are not allowed"
417+
'mixed numeric and non-numeric field names are not allowed'
422418
);
423419
}
424420
return xdr.ScVal.scvVec(
@@ -430,7 +426,7 @@ export class ContractSpec {
430426
let name = field.name().toString();
431427
return new xdr.ScMapEntry({
432428
key: this.nativeToScVal(name, xdr.ScSpecTypeDef.scSpecTypeSymbol()),
433-
val: this.nativeToScVal(val[name], field.type()),
429+
val: this.nativeToScVal(val[name], field.type())
434430
});
435431
})
436432
);
@@ -453,7 +449,7 @@ export class ContractSpec {
453449
* @throws {Error} if ScVal cannot be converted to the given type
454450
*/
455451
scValStrToNative<T>(scv: string, typeDef: xdr.ScSpecTypeDef): T {
456-
return this.scValToNative<T>(xdr.ScVal.fromXDR(scv, "base64"), typeDef);
452+
return this.scValToNative<T>(xdr.ScVal.fromXDR(scv, 'base64'), typeDef);
457453
}
458454

459455
/**
@@ -518,12 +514,16 @@ export class ContractSpec {
518514
return new Map(
519515
map.map((entry) => [
520516
this.scValToNative(entry.key(), keyType),
521-
this.scValToNative(entry.val(), valueType),
517+
this.scValToNative(entry.val(), valueType)
522518
])
523519
) as T;
524520
}
525521
throw new TypeError(
526-
`ScSpecType ${t.name} was not map, but ${JSON.stringify(scv, null, 2)} is`
522+
`ScSpecType ${t.name} was not map, but ${JSON.stringify(
523+
scv,
524+
null,
525+
2
526+
)} is`
527527
);
528528
}
529529

@@ -541,7 +541,9 @@ export class ContractSpec {
541541
value !== xdr.ScSpecType.scSpecTypeSymbol().value
542542
) {
543543
throw new Error(
544-
`ScSpecType ${t.name} was not string or symbol, but ${JSON.stringify(scv, null, 2)} is`
544+
`ScSpecType ${
545+
t.name
546+
} was not string or symbol, but ${JSON.stringify(scv, null, 2)} is`
545547
);
546548
}
547549
return scv.value()?.toString() as T;
@@ -555,7 +557,11 @@ export class ContractSpec {
555557
// in the fallthrough case, just return the underlying value directly
556558
default:
557559
throw new TypeError(
558-
`failed to convert ${JSON.stringify(scv, null, 2)} to native type from type ${t.name}`
560+
`failed to convert ${JSON.stringify(
561+
scv,
562+
null,
563+
2
564+
)} to native type from type ${t.name}`
559565
);
560566
}
561567
}
@@ -656,9 +662,7 @@ function stringToScVal(str: string, ty: xdr.ScSpecType): xdr.ScVal {
656662
}
657663

658664
default:
659-
throw new TypeError(
660-
`invalid type ${ty.name} specified for string value`
661-
);
665+
throw new TypeError(`invalid type ${ty.name} specified for string value`);
662666
}
663667
}
664668

src/index.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/* tslint:disable:no-var-requires */
22

33
// Expose all types
4-
export * from "./soroban_rpc";
5-
export { ContractSpec } from "./contract_spec";
4+
export * from './soroban_rpc';
5+
export { ContractSpec } from './contract_spec';
66

77
// stellar-sdk classes to expose
8-
export { Server, Durability } from "./server";
9-
export { AxiosClient, version } from "./axios";
10-
export * from "./transaction";
8+
export { Server, Durability } from './server';
9+
export { AxiosClient, version } from './axios';
10+
export * from './transaction';
1111

1212
// export is necessary for testing, but it may be useful for others
13-
export { parseRawSimulation, parseRawEvents } from "./parsers";
13+
export { parseRawSimulation, parseRawEvents } from './parsers';
1414

1515
// expose classes and functions from stellar-base
16-
export * from "stellar-base";
16+
export * from 'stellar-base';
1717

1818
export default module.exports;

src/jsonrpc.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
import axios from "./axios";
1+
import axios from './axios';
22

33
export type Id = string | number;
44

55
export interface Request<T> {
6-
jsonrpc: "2.0";
6+
jsonrpc: '2.0';
77
id: Id;
88
method: string;
99
params: T;
1010
}
1111

1212
export interface Notification<T> {
13-
jsonrpc: "2.0";
13+
jsonrpc: '2.0';
1414
method: string;
1515
params?: T;
1616
}
1717

1818
export type Response<T, E = any> = {
19-
jsonrpc: "2.0";
19+
jsonrpc: '2.0';
2020
id: Id;
2121
} & ({ error: Error<E> } | { result: T });
2222

@@ -38,13 +38,13 @@ export async function post<T>(
3838
params = null;
3939
}
4040
const response = await axios.post<Response<T>>(url, {
41-
jsonrpc: "2.0",
41+
jsonrpc: '2.0',
4242
// TODO: Generate a unique request id
4343
id: 1,
4444
method,
45-
params,
45+
params
4646
});
47-
if (hasOwnProperty(response.data, "error")) {
47+
if (hasOwnProperty(response.data, 'error')) {
4848
throw response.data.error;
4949
} else {
5050
return response.data?.result;
@@ -57,16 +57,16 @@ export async function post<T>(
5757
export async function postObject<T>(
5858
url: string,
5959
method: string,
60-
param: any,
60+
param: any
6161
): Promise<T> {
6262
const response = await axios.post<Response<T>>(url, {
63-
jsonrpc: "2.0",
63+
jsonrpc: '2.0',
6464
// TODO: Generate a unique request id
6565
id: 1,
6666
method,
67-
params: param,
67+
params: param
6868
});
69-
if (hasOwnProperty(response.data, "error")) {
69+
if (hasOwnProperty(response.data, 'error')) {
7070
throw response.data.error;
7171
} else {
7272
return response.data?.result;
@@ -77,7 +77,7 @@ export async function postObject<T>(
7777
// typescript typing.
7878
function hasOwnProperty<X extends {}, Y extends PropertyKey>(
7979
obj: X,
80-
prop: Y,
80+
prop: Y
8181
): obj is X & Record<Y, unknown> {
8282
return obj.hasOwnProperty(prop);
8383
}

0 commit comments

Comments
 (0)