Skip to content

Commit fe3893f

Browse files
authored
Merge pull request #1168 from IntersectMBO/improve-comment-for-object-type
cardano-wasm: Add examples of object type names to objectType typescript documentation
2 parents 6617099 + 87153f4 commit fe3893f

6 files changed

Lines changed: 41 additions & 6 deletions

File tree

cardano-wasm/lib-wrapper/cardano-api.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import Wallet from './wallet';
1212
declare interface CardanoApi {
1313
/**
1414
* The type of the object, used for identification (the "CardanoApi" string).
15+
* Other types of objects would be:
16+
* "GrpcConnection", "SignedTx", "UnsignedTx", and "Wallet"
1517
*/
1618
objectType: string;
1719

cardano-wasm/lib-wrapper/grpc-connection.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
declare interface GrpcConnection {
77
/**
88
* The type of the object, used for identification (the "GrpcConnection" string).
9+
* Other types of objects would be:
10+
* "CardanoApi", "SignedTx", "UnsignedTx", and "Wallet"
911
*/
1012
objectType: string;
1113

cardano-wasm/lib-wrapper/signed-tx.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
declare interface SignedTx {
77
/**
88
* The type of the object, used for identification (the "SignedTx" string).
9+
* Other types of objects would be:
10+
* "CardanoApi", "GrpcConnection", "UnsignedTx", and "Wallet"
911
*/
1012
objectType: string;
1113

cardano-wasm/lib-wrapper/unsigned-tx.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import SignedTx from './signed-tx';
88
declare interface UnsignedTx {
99
/**
1010
* The type of the object, used for identification (the "UnsignedTx" string).
11+
* Other types of objects would be:
12+
* "CardanoApi", "GrpcConnection", "SignedTx", and "Wallet"
1113
*/
1214
objectType: string;
1315

cardano-wasm/lib-wrapper/wallet.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
declare interface Wallet {
77
/**
88
* The type of the object, used for identification (the "Wallet" string).
9+
* Other types of objects would be:
10+
* "CardanoApi", "GrpcConnection", "SignedTx", and "UnsignedTx"
911
*/
1012
objectType: string;
1113

cardano-wasm/src-lib/Cardano/Wasm/Api/InfoToTypeScript.hs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ apiInfoToTypeScriptFile apiInfo =
1616
{ TypeScript.typeScriptFileName = Info.dashCaseName (Info.mainObject apiInfo) <> ".d.ts"
1717
, TypeScript.typeScriptFileContent =
1818
virtualObjectInfoToInterfaceDecs
19+
mainObjectName
1920
False
2021
voMap
2122
(Info.mainObject apiInfo)
@@ -38,10 +39,12 @@ apiInfoToTypeScriptFile apiInfo =
3839
: virtualObjectInterfaces
3940
where
4041
virtualObjectInterfaces =
41-
map (virtualObjectInfoToTypeScriptFile voMap) (Info.virtualObjects apiInfo)
42+
map (virtualObjectInfoToTypeScriptFile mainObjectName voMap) (Info.virtualObjects apiInfo)
4243

4344
voMap = Map.fromList [(Info.virtualObjectName vo, vo) | vo <- Info.virtualObjects apiInfo]
4445

46+
mainObjectName = Info.virtualObjectName $ Info.mainObject apiInfo
47+
4548
importDeclaration :: Info.VirtualObjectInfo -> TypeScript.Declaration
4649
importDeclaration vo =
4750
TypeScript.Declaration
@@ -69,16 +72,27 @@ flattenMethods = concatMap flattenMethods'
6972
flattenMethods' (Info.MethodGroupEntry (Info.MethodGroup{Info.groupMethods = methods})) = flattenMethods methods
7073

7174
virtualObjectInfoToTypeScriptFile
72-
:: Map String Info.VirtualObjectInfo -> Info.VirtualObjectInfo -> TypeScript.TypeScriptFile
73-
virtualObjectInfoToTypeScriptFile voMap vo =
75+
:: String -> Map String Info.VirtualObjectInfo -> Info.VirtualObjectInfo -> TypeScript.TypeScriptFile
76+
virtualObjectInfoToTypeScriptFile mainObjectName voMap vo =
7477
TypeScript.TypeScriptFile
7578
{ TypeScript.typeScriptFileName = Info.dashCaseName vo <> ".d.ts"
76-
, TypeScript.typeScriptFileContent = virtualObjectInfoToInterfaceDecs True voMap vo
79+
, TypeScript.typeScriptFileContent = virtualObjectInfoToInterfaceDecs mainObjectName True voMap vo
7780
}
7881

82+
oxfordCommaSeparatedList :: [String] -> String
83+
oxfordCommaSeparatedList [] = ""
84+
oxfordCommaSeparatedList [l] = l
85+
oxfordCommaSeparatedList [p, l] = p <> " and " <> l
86+
oxfordCommaSeparatedList [a, p, l] = a <> ", " <> p <> ", and " <> l
87+
oxfordCommaSeparatedList (h : t) = h <> ", " <> oxfordCommaSeparatedList t
88+
7989
virtualObjectInfoToInterfaceDecs
80-
:: Bool -> Map String Info.VirtualObjectInfo -> Info.VirtualObjectInfo -> [TypeScript.Declaration]
81-
virtualObjectInfoToInterfaceDecs isDefaultExport voMap vo =
90+
:: String
91+
-> Bool
92+
-> Map String Info.VirtualObjectInfo
93+
-> Info.VirtualObjectInfo
94+
-> [TypeScript.Declaration]
95+
virtualObjectInfoToInterfaceDecs mainObjectName isDefaultExport voMap vo =
8296
importDeclarations voMap vo
8397
++ [ TypeScript.Declaration
8498
[Info.virtualObjectDoc vo]
@@ -89,6 +103,17 @@ virtualObjectInfoToInterfaceDecs isDefaultExport voMap vo =
89103
[ "The type of the object, used for identification (the \""
90104
<> Info.virtualObjectName vo
91105
<> "\" string)."
106+
, "Other types of objects would be:"
107+
, oxfordCommaSeparatedList
108+
( ( if mainObjectName == Info.virtualObjectName vo
109+
then id
110+
else (show mainObjectName :)
111+
)
112+
[ show $ Info.virtualObjectName v
113+
| v <- Map.elems voMap
114+
, Info.virtualObjectName v /= Info.virtualObjectName vo
115+
]
116+
)
92117
]
93118
(TypeScript.InterfaceProperty "objectType" "string")
94119
]

0 commit comments

Comments
 (0)