Skip to content

Commit

Permalink
Merge pull request #343 from osmosis-labs/fix/template-strings
Browse files Browse the repository at this point in the history
Fix/template strings
  • Loading branch information
pyramation authored Apr 7, 2023
2 parents 2a06895 + e1da910 commit 4a767d7
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 39 deletions.
103 changes: 64 additions & 39 deletions packages/ast/src/clients/lcd/class/lcd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,37 +71,37 @@ const returnAwaitRequest = (
}

let returned: t.Expression = t.awaitExpression(
callExpression(
t.memberExpression(
t.memberExpression(t.thisExpression(), t.identifier('req')),
t.identifier('get')
),
args,
t.tsTypeParameterInstantiation([
t.tsTypeReference(
t.identifier(getResponseTypeName(context, responseType))
)
])
)
callExpression(
t.memberExpression(
t.memberExpression(t.thisExpression(), t.identifier('req')),
t.identifier('get')
),
args,
t.tsTypeParameterInstantiation([
t.tsTypeReference(
t.identifier(getResponseTypeName(context, responseType))
)
])
)
);

if (context.pluginValue('useSDKTypes') &&
context.pluginValue('prototypes.methods.fromSDKJSON')) {
//useSDKTypes && prototypes.methods.fromSDKJSON
returned = t.callExpression(
t.memberExpression(
t.identifier(responseType),
t.identifier('fromSDKJSON')
),
[returned]
);
context.pluginValue('prototypes.methods.fromSDKJSON')) {
//useSDKTypes && prototypes.methods.fromSDKJSON
returned = t.callExpression(
t.memberExpression(
t.identifier(responseType),
t.identifier('fromSDKJSON')
),
[returned]
);
} else if (!context.pluginValue('useSDKTypes') &&
context.pluginValue('prototypes.methods.fromJSON')) {
//!useSDKTypes && prototypes.methods.fromJSON
returned = t.callExpression(
t.memberExpression(t.identifier(responseType), t.identifier('fromJSON')),
[returned]
);
context.pluginValue('prototypes.methods.fromJSON')) {
//!useSDKTypes && prototypes.methods.fromJSON
returned = t.callExpression(
t.memberExpression(t.identifier(responseType), t.identifier('fromJSON')),
[returned]
);
}

return t.returnStatement(returned);
Expand Down Expand Up @@ -253,35 +253,60 @@ export const getUrlTemplateString = (url: string) => {
};
};

const routeRegexForReplace = /[^\{\}\\-\_\\.$/a-zA-Z0-9]+/g;
export const makeTemplateTag = (info: ProtoServiceMethodInfo) => {
const route = info.url
.split('/')
.filter(a => a !== '')
.map(a => {
if (a.startsWith('{')) {
return `$${a}`;
// clean weird routes like this one:
// /ibc/apps/transfer/v1/denom_traces/{hash=**}
return `$${a}`
.replace(routeRegexForReplace, '')
.replace('{', `{params.`)
} else {
return a;
}
})
.join('/');

// clean route here

const parsed = getAstFromString(`\`${route}\``);
// @ts-ignore
const ast: t.TemplateLiteral = parsed.program.body[0].expression;

ast.expressions = ast.expressions.map((identifier: t.Identifier) => {
const name = info.casing?.[identifier.name] ? info.casing[identifier.name] : identifier.name;
if (!name) {
console.warn(route);
console.warn('route type not yet supported');
return;
ast.expressions = ast.expressions.map((expr: t.MemberExpression) => {
let name;
switch (expr.object.type) {
case 'MemberExpression': {
// e.g. params.thing.another
const memberExpr: t.MemberExpression = expr.object;
// @ts-ignore
name = memberExpr.property.name;
name = info.casing?.[name] ? info.casing[name] : name;
// @ts-ignore
expr.object.property.name = name;
break;
}
case 'Identifier': {
// e.g. params.thing
// @ts-ignore
const identifier: t.Identifier = expr.property;
name = identifier.name;
name = info.casing?.[name] ? info.casing[name] : name;
// @ts-ignore
expr.property.name = name;
break;
}
case 'Identifier':
break;
default:
throw new Error('unknown expression type in route parsing');
}
return t.memberExpression(
t.identifier('params'),
t.identifier(name)
)
}).filter(Boolean)
return expr;
}).filter(Boolean);
return ast;
};

Expand Down
28 changes: 28 additions & 0 deletions packages/ast/src/clients/lcd/class/template-tag.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,34 @@ it('/cosmos/gov/v1beta1/proposals/{proposal_id}/votes/{voter}', () => {
expect(getUrlTemplateString('/cosmos/gov/v1beta1/proposals/{proposal_id}/votes/{voter}')).toMatchSnapshot();
})

/////
// UNIQUE CASES


it('/ibc/apps/transfer/v1/denom_traces/{hash=**}', () => {
expect(renderTemplateTag({
method: 'get',
url: '/ibc/apps/transfer/v1/denom_traces/{hash=**}',
pathParams: ['hash'],
queryParams: [],
paramMap: { hash: 'hash' },
casing: { hash: 'hash' }
})).toEqual("`ibc/apps/transfer/v1/denom_traces/${params.hash}`")
})

it('/ibc/apps/fee/v1/channels/{packet_id.channel_id}/ports/{packet_id.port_id}/sequences/{packet_id.sequence}/incentivized_packet', () => {
expect(renderTemplateTag({
method: 'get',
url: '/ibc/apps/fee/v1/channels/{packet_id.channel_id}/ports/{packet_id.port_id}/sequences/{packet_id.sequence}/incentivized_packet',
pathParams: ['packet_id'],
queryParams: [],
paramMap: { packet_id: 'packet_id' },
casing: { packet_id: 'packet_id' }
})).toEqual("`ibc/apps/fee/v1/channels/${params.packet_id.channel_id}/ports/${params.packet_id.port_id}/sequences/${params.packet_id.sequence}/incentivized_packet`")
})


/////

it('1', () => {
expect(renderTemplateTag({
Expand Down

0 comments on commit 4a767d7

Please sign in to comment.