ryde/source/logic/helpers/nextbike.ts

212 lines
2.7 KiB
TypeScript
Raw Permalink Normal View History

2026-03-06 08:37:53 +01:00
namespace lib_nextbike
{
/**
*/
let _baseurl : (null | string) = null;
/**
*/
let _apikey : (null | string) = null;
/**
*/
let _testmode : boolean = false;
/**
*/
export function setup
(
baseurl : string,
apikey : string,
options :
{
testmode ?: boolean;
} = {}
) : void
{
options = Object.assign
(
{
"testmode": false,
},
options
);
_baseurl = baseurl;
_apikey = apikey;
_testmode = options.testmode;
}
/**
*/
async function api_call
(
action : string,
loginkey : (null | string),
input : Record<string, string>
) : Promise<any>
{
if ((_baseurl === null) || (_apikey === null))
{
throw (new Error("API not set up yet; execute 'setup' function"));
}
else
{
if (_testmode)
{
return {
"user": {
"loginkey": "",
},
};
}
else
{
return (
fetch
(
lib_string.coin
(
"{{baseurl}}?{{query}}",
{
"baseurl": _baseurl,
"query": lib_call.convey
(
{
"version": "v2",
"format": "json",
"apikey": _apikey,
"action": action,
"loginkey": loginkey,
},
[
Object.entries,
x => x.filter(([key, value]) => (value !== null)),
x => x.map(([key, value]) => lib_string.coin("{{key}}={{value}}", {"key": key, "value": value})),
x => x.join("&")
]
),
}
),
{
"method": "POST",
"body": JSON.stringify(input),
}
)
.then
(
response => response.json()
)
);
}
}
}
/**
*/
export async function login
(
mobile : string,
pin : string
) : Promise<any>
{
return api_call
(
"login",
null,
{"mobile": mobile, "pin": pin}
);
}
/**
*/
export async function logout
(
loginkey : string
) : Promise<any>
{
return api_call
(
"logout",
loginkey,
{}
);
}
/**
*/
export async function open_lock
(
loginkey : string,
bike : string
) : Promise<any>
{
return api_call
(
"openLock",
loginkey,
{"bike": bike}
);
}
/**
*/
export async function rental_begin
(
loginkey : string,
bike : string
) : Promise<any>
{
return api_call
(
"rent",
loginkey,
{"bike": bike}
);
}
/**
*/
export async function rental_break
(
loginkey : string,
bike : string
) : Promise<any>
{
return api_call
(
"rentalBreak",
loginkey,
{"bike": bike}
);
}
/**
*/
export async function rental_end
(
loginkey : string,
bike : string
) : Promise<any>
{
return api_call
(
"return",
loginkey,
{"bike": bike}
);
}
}