feat(napi-derive): add ts_arg_type attribute to override individual args on functions (#1192)
This commit is contained in:
parent
5cbeac59dc
commit
5be415d3d9
10 changed files with 180 additions and 25 deletions
examples/napi
|
@ -105,6 +105,8 @@ Generated by [AVA](https://avajs.dev).
|
|||
export function validateString(s: string): string␊
|
||||
export function validateSymbol(s: symbol): boolean␊
|
||||
export function tsRename(a: { foo: number }): string[]␊
|
||||
export function overrideIndividualArgOnFunction(notOverridden: string, f: () => string, notOverridden2: number): string␊
|
||||
export function overrideIndividualArgOnFunctionWithCbArg(callback: (town: string, name?: string | undefined | null) => string, notOverridden: number): object␊
|
||||
export function xxh64Alias(input: Buffer): bigint␊
|
||||
export function getMapping(): Record<string, number>␊
|
||||
export function sumMapping(nums: Record<string, number>): number␊
|
||||
|
@ -208,6 +210,7 @@ Generated by [AVA](https://avajs.dev).
|
|||
*/␊
|
||||
returnOtherClass(): Dog␊
|
||||
returnOtherClassWithCustomConstructor(): Bird␊
|
||||
overrideIndividualArgOnMethod(normalTy: string, overriddenTy: {n: string}): Bird␊
|
||||
}␊
|
||||
export class Dog {␊
|
||||
name: string␊
|
||||
|
|
Binary file not shown.
|
@ -95,6 +95,8 @@ import {
|
|||
callbackReturnPromise,
|
||||
returnEitherClass,
|
||||
eitherFromOption,
|
||||
overrideIndividualArgOnFunction,
|
||||
overrideIndividualArgOnFunctionWithCbArg,
|
||||
} from '../'
|
||||
|
||||
test('export const', (t) => {
|
||||
|
@ -157,6 +159,10 @@ test('class', (t) => {
|
|||
t.is(dog.name, '可乐')
|
||||
t.deepEqual(dog.returnOtherClass(), new Dog('Doge'))
|
||||
t.deepEqual(dog.returnOtherClassWithCustomConstructor(), new Bird('parrot'))
|
||||
t.is(
|
||||
dog.overrideIndividualArgOnMethod('Jafar', { n: 'Iago' }).name,
|
||||
'Jafar-Iago',
|
||||
)
|
||||
t.is(dog.returnOtherClassWithCustomConstructor().getCount(), 1234)
|
||||
t.is(dog.type, Kind.Dog)
|
||||
dog.type = Kind.Cat
|
||||
|
@ -321,6 +327,20 @@ test('function ts type override', (t) => {
|
|||
t.deepEqual(tsRename({ foo: 1, bar: 2, baz: 2 }), ['foo', 'bar', 'baz'])
|
||||
})
|
||||
|
||||
test('function individual ts arg type override', (t) => {
|
||||
t.is(
|
||||
overrideIndividualArgOnFunction('someStr', () => 'anotherStr', 42),
|
||||
'oia: someStr-42-anotherStr',
|
||||
)
|
||||
t.deepEqual(
|
||||
overrideIndividualArgOnFunctionWithCbArg(
|
||||
(town, opt) => `im: ${town}-${opt}`,
|
||||
89,
|
||||
),
|
||||
'im: World(89)-null',
|
||||
)
|
||||
})
|
||||
|
||||
test('option object', (t) => {
|
||||
t.notThrows(() => receiveAllOptionalObject())
|
||||
t.notThrows(() => receiveAllOptionalObject({}))
|
||||
|
|
3
examples/napi/index.d.ts
vendored
3
examples/napi/index.d.ts
vendored
|
@ -95,6 +95,8 @@ export function validatePromise(p: Promise<number>): Promise<number>
|
|||
export function validateString(s: string): string
|
||||
export function validateSymbol(s: symbol): boolean
|
||||
export function tsRename(a: { foo: number }): string[]
|
||||
export function overrideIndividualArgOnFunction(notOverridden: string, f: () => string, notOverridden2: number): string
|
||||
export function overrideIndividualArgOnFunctionWithCbArg(callback: (town: string, name?: string | undefined | null) => string, notOverridden: number): object
|
||||
export function xxh64Alias(input: Buffer): bigint
|
||||
export function getMapping(): Record<string, number>
|
||||
export function sumMapping(nums: Record<string, number>): number
|
||||
|
@ -198,6 +200,7 @@ export class Animal {
|
|||
*/
|
||||
returnOtherClass(): Dog
|
||||
returnOtherClassWithCustomConstructor(): Bird
|
||||
overrideIndividualArgOnMethod(normalTy: string, overriddenTy: {n: string}): Bird
|
||||
}
|
||||
export class Dog {
|
||||
name: string
|
||||
|
|
|
@ -85,6 +85,18 @@ impl Animal {
|
|||
pub fn return_other_class_with_custom_constructor(&self) -> Bird {
|
||||
Bird::new("parrot".to_owned())
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub fn override_individual_arg_on_method(
|
||||
&self,
|
||||
normal_ty: String,
|
||||
#[napi(ts_arg_type = "{n: string}")] overridden_ty: napi::JsObject,
|
||||
) -> Bird {
|
||||
let obj = overridden_ty.coerce_to_object().unwrap();
|
||||
let the_n: Option<String> = obj.get("n").unwrap();
|
||||
|
||||
Bird::new(format!("{}-{}", normal_ty, the_n.unwrap()))
|
||||
}
|
||||
}
|
||||
|
||||
#[napi(constructor)]
|
||||
|
|
|
@ -1,6 +1,36 @@
|
|||
use napi::bindgen_prelude::{Object, Result};
|
||||
use napi::JsFunction;
|
||||
|
||||
#[napi(ts_args_type = "a: { foo: number }", ts_return_type = "string[]")]
|
||||
fn ts_rename(a: Object) -> Result<Object> {
|
||||
a.get_property_names()
|
||||
}
|
||||
|
||||
#[napi]
|
||||
fn override_individual_arg_on_function(
|
||||
not_overridden: String,
|
||||
#[napi(ts_arg_type = "() => string")] f: JsFunction,
|
||||
not_overridden2: u32,
|
||||
) -> String {
|
||||
let u = f.call_without_args(None).unwrap();
|
||||
let s = u
|
||||
.coerce_to_string()
|
||||
.unwrap()
|
||||
.into_utf8()
|
||||
.unwrap()
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.to_string();
|
||||
|
||||
format!("oia: {}-{}-{}", not_overridden, not_overridden2, s)
|
||||
}
|
||||
|
||||
#[napi]
|
||||
fn override_individual_arg_on_function_with_cb_arg<
|
||||
T: Fn(String, Option<String>) -> Result<Object>,
|
||||
>(
|
||||
#[napi(ts_arg_type = "(town: string, name?: string | undefined | null) => string")] callback: T,
|
||||
not_overridden: u32,
|
||||
) -> Result<Object> {
|
||||
callback(format!("World({})", not_overridden), None)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue