Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions attw.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"no-resolution",
"cjs-only-exports-default",
"unexpected-module-syntax",
"cjs-resolves-to-esm"
"cjs-resolves-to-esm",
"named-exports"
],
"failingPackages": [
"af-utils__react-table",
Expand All @@ -14,7 +15,6 @@
"amqplib",
"angular-gridster",
"apca-w3",
"app-module-path",
"architect",
"axios-cancel",
"babel-plugin-syntax-jsx",
Expand All @@ -37,7 +37,6 @@
"cloudfour__simple-svg-placeholder",
"collectionsjs",
"combine-reducers",
"conditional",
"cornerstone-core",
"country-flag-icons",
"critters-webpack-plugin",
Expand Down Expand Up @@ -249,7 +248,6 @@
"react-native-signature-capture",
"react-native-svg-animated-linear-gradient",
"react-paginate",
"react-paginate/v5",
"react-payment-inputs",
"react-plyr",
"react-recaptcha-v3",
Expand Down
81 changes: 76 additions & 5 deletions types/espruino/espruino-tests.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,86 @@
import * as wifi from "Wifi";

wifi.connect("ssid", { password: "pass", authMode: "wpa_wpa2" }, err => {
if (err) throw err;
console.log("connected");
// minimum
wifi.connect("Test", {});

wifi.connect("Test", {
password: "Test1234",
authMode: "wpa_wpa2",
channel: 6,
bssid: "aa:bb:cc:dd:ee:ff",
dnsServers: ["8.8.8.8", "1.1.1.1"],
});

wifi.connect("Test", { password: "pw" }, err => {
if (err === null) {
console.log("connected");
} else {
const reason: string = err;
console.log("failed:", reason);
}
});

// test all types of auth
(["open", "wpa", "wpa2", "wpa_wpa2"] as const).forEach(authMode => {
wifi.connect("Test", { authMode });
});

wifi.startAP("ssid", { password: "pass", authMode: "wpa_wpa2" }, err => {
// @ts-expect-error -- "wep" is not a valid WifiAuth
wifi.connect("Test", { authMode: "wep" });

// @ts-expect-error -- ssid must be a string
wifi.connect(12345, {});

// @ts-expect-error -- options is required (not optional in current d.ts)
wifi.connect("Test");

// ALLOWED
wifi.connect("Test", { dnsServers: ["8.8.8.8", "1.1.1.1"] });

// @ts-expect-error -- dnsServers is a 2-tuple, not a 3-tuple
wifi.connect("Test", { dnsServers: ["8.8.8.8", "1.1.1.1", "9.9.9.9"] });

// @ts-expect-error -- unknown option
wifi.connect("Test", { encryption: "wpa2" });

// @ts-expect-error -- callback err parameter is string|null, not Error
wifi.connect("Test", {}, (err: Error | null) => {});

// AP STUFF
wifi.startAP("Test", {
password: "Test1234",
authMode: "wpa_wpa2",
channel: 11,
hidden: true,
}, err => {
if (err) throw err;
console.log("created");
console.log("AP up");
});
wifi.stopAP(() => {});

// minimum
wifi.startAP("MyAP", {}, () => {});
wifi.stopAP(() => {});

// @ts-expect-error -- callback is required
wifi.startAP("MyAP", { password: "pw" });

// @ts-expect-error -- hidden must be boolean
wifi.startAP("MyAP", { hidden: "yes" }, () => {});

// @ts-expect-error -- channel must be number
wifi.startAP("MyAP", { channel: "6" }, () => {});

wifi.disconnect(() => {
console.log("disconnected");
});

// @ts-expect-error -- callback is required
wifi.disconnect();

// @ts-expect-error -- callback takes no arguments
wifi.disconnect((err: string) => {});

digitalWrite(D2, false);
digitalWrite(D2, true);

Expand Down
174 changes: 170 additions & 4 deletions types/espruino/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,98 @@ declare interface Object {
}

declare module "Wifi" {
function connect(ssid: string, options: any, callback: (err: any) => any): any;
function startAP(ssid: string, options: any, callback: (err: any) => any): any;
type status = "off" | "connecting" | "wrong_password" | "no_ap_found" | "connect_fail" | "connected";
type WifiAuth = "open" | "wpa2" | "wpa" | "wpa_wpa2";
interface APOptions {
authMode?: WifiAuth;
password?: string;
channel?: number;
hidden?: boolean;
}

interface connectionOptions {
password?: string;
dnsServers?: [string, string]; // Max 2 DNS servers
channel?: number;
bssid?: string;
authMode?: WifiAuth;
}

function connect(ssid: string, options: connectionOptions, callback?: (err: string | null) => void): void;
function startAP(ssid: string, options: APOptions, callback: (err: string | null) => void): void;
function disconnect(callback: () => void): void;

function getAPDetails(callback?: (details: any) => void): {
status: "enabled" | "disabled";
stations: { ip: any }[];
ssid: string;
password: string;
authMode: WifiAuth;
hidden: boolean;
maxConn: number;
savedSsid: string | null;
};

interface IPInfo {
ip: string;
netmask: string;
gw: string;
mac: string;
}
function getAPIP(callback?: (err: any, ipinfo: IPInfo) => void): IPInfo;

interface details {
status: status;
rssi: any;
ssid: string;
password: string;
authMode: WifiAuth;
savedSsid: string;
}
function getDetails(callback?: (details: any) => void): details;

function getHostByName(hostname: string, callback: (ip: string) => void): string;
function getHostname(callback?: (hostname: string) => void): string;
function getIP(callback?: (err: any, ipinfo: IPInfo) => void): IPInfo;

interface StatusCallback {
status: status;
ap: "enabled" | "disabled";
mode: "off" | "sta" | "ap" | "sta+ap";
phy: "11b" | "11g" | "11n";
powersave: "none" | "ps-poll";
savedMode: "off" | "sta" | "ap" | "sta+ap";
}
function getStatus(callback?: (status: StatusCallback) => void): StatusCallback;

function ping(hostname: string, callback: (time: string | number) => void): void;
function restore(): void;
function save(what: "clear" | "sta+ap"): void;
function scan(
callback: (
err: string | null,
ap_list: { ssid: string; mac: string; authMode: WifiAuth; channel: number; rssi: number | string }[],
) => void,
): void;
function setAPIP(settings: { ip: string; gw: string; netmask: string }, callback: (err: string) => void): void;
function setConfig(settings: { phy: "11b" | "11g" | "11n"; powersave: "none" | "ps-poll" }): void;
function setHostname(hostname: string, callback?: () => void): void;
function setIP(settings: { ip: string; gw: string; netmask: string }, callback: (err: string) => void): void;
function setSNTP(server: string, tz_offset: number): void;
function stopAP(callback: () => void): void;
function turbo(enable: boolean | number, callback: () => void): void;

// EVENTS
type events =
| "connected"
| "dhcp_timeout"
| "disconnected"
| "probe_recv"
| "sta_joined"
| "sta_left"
| "associated"
| "auth_change";
function on(event: events, callback: (details: any) => void): void;
}

declare module "InfluxDB" {
Expand Down Expand Up @@ -2105,8 +2195,10 @@ declare namespace ESP8266 {
*/
declare function http(): void;

type HTTPRequestMethod = "GET" | "POST" | "PUT" | "DELETE";

/** */
declare namespace http {
declare module "http" {
/**
* <p>Create an HTTP Server</p>
* <p>When a request to the server is made, the callback is called. In the callback you can use the methods on the response (httpSRs) to send data. You can also add <code>request.on(&#39;data&#39;,function() { ... })</code> to listen for POSTed data</p>
Expand All @@ -2116,6 +2208,36 @@ declare namespace http {
* @url http://www.espruino.com/Reference#l_http_createServer
*/
function createServer(callback: any): httpSrv;

/**
* <p>Retrieve Data from a Remote Server</p>
*
* @param options
* @param callback
* @return
* @url https://www.espruino.com/Reference#t_l_http_get
*/
function get(options: string, callback: (data: httpCRs) => void): httpCRq;

/**
* <p>Retrieve/Put Data to a Server</p>
*
* @param options
* @param callback
* @return
* @url https://www.espruino.com/Reference#t_l_http_request
*/
function request(
options: {
host: string;
port: number;
path: string;
method: HTTPRequestMethod;
protocol: "https:" | "http:";
headers: Record<string, any>;
},
callback: (data: httpCRs) => void,
): httpCRq;
}

/**
Expand Down Expand Up @@ -2150,12 +2272,17 @@ declare interface httpSrv {
*
* @url http://www.espruino.com/Reference#httpSRq
*/
type httpSRqEvent = "close" | "drain";
declare interface httpSRq {
/**
* @return
*/
new(): httpSRq;

url: string;
headers: Record<string, any>;
method: HTTPRequestMethod;

/**
* <p>Return how many bytes are available to read. If there is already a listener for data, this will always return 0.</p>
*
Expand All @@ -2181,19 +2308,30 @@ declare interface httpSRq {
* @url http://www.espruino.com/Reference#l_httpSRq_pipe
*/
pipe(destination: any, options: any): void;

/**
* <p>Event Listener</p>
* @param event
* @param callback
*/
on(event: httpSRqEvent, callback: (data: any) => void): void;
}

/**
* <p>The HTTP server response</p>
*
* @url http://www.espruino.com/Reference#httpSRs
*/
type httpSRsEvent = "close" | "data";
declare interface httpSRs {
/**
* @return
*/
new(): httpSRs;

headers: Record<string, any>;
setHeader(key: string, value: string): void;

/**
* <p>This function writes the <code>data</code> argument as a string. Data that is passed in
* (including arrays) will be converted to a string with the normal JavaScript
Expand All @@ -2218,14 +2356,22 @@ declare interface httpSRs {
* @param headers
* @url http://www.espruino.com/Reference#l_httpSRs_writeHead
*/
writeHead(statusCode: number, headers: any): void;
writeHead(statusCode: number | string, headers: Record<string, any>): void;

/**
* <p>Event Listener</p>
* @param event
* @param callback
*/
on(event: httpSRsEvent, callback: (data: any) => void): void;
}

/**
* <p>The HTTP client request, returned by <code>http.request()</code> and <code>http.get()</code>.</p>
*
* @url http://www.espruino.com/Reference#httpCRq
*/
type httpCRqEvent = "drain" | "error";
declare interface httpCRq {
/**
* @return
Expand All @@ -2251,19 +2397,32 @@ declare interface httpCRq {
* @url http://www.espruino.com/Reference#l_httpCRq_end
*/
end(data: any): void;

/**
* <p>Event Listener</p>
* @param event
* @param callback
*/
on(event: httpCRqEvent, callback: (data: any) => void): void;
}

/**
* <p>The HTTP client response, passed to the callback of <code>http.request()</code> an <code>http.get()</code>.</p>
*
* @url http://www.espruino.com/Reference#httpCRs
*/
type httpCRsEvent = "close" | "data" | "error";
declare interface httpCRs {
/**
* @return
*/
new(): httpCRs;

headers: Record<string, any>;
httpVersion: string;
statusCode: string;
statusMessage: string;

/**
* <p>Return how many bytes are available to read. If there is a &#39;data&#39; event handler, this will always return 0.</p>
*
Expand All @@ -2289,6 +2448,13 @@ declare interface httpCRs {
* @url http://www.espruino.com/Reference#l_httpCRs_pipe
*/
pipe(destination: any, options: any): void;

/**
* <p>Event Listener</p>
* @param event
* @param callback
*/
on(event: httpCRsEvent, callback: (data: any) => void): void;
}

/**
Expand Down
Loading