Skip to content
This repository has been archived by the owner on Apr 16, 2021. It is now read-only.

Commit

Permalink
Fix eslint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcnski committed Feb 25, 2021
1 parent 010aaab commit 8690cf2
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
],
"scripts": {
"build": "rimraf dist && yarn run webpack",
"lint:eslint": "eslint --ext .ts scripts src utils --max-warnings 0",
"lint:eslint": "eslint --ext .ts src utils --max-warnings 0",
"lint:tsc": "tsc",
"prepublishOnly": "yarn build",
"test": "jest --coverage --coverageDirectory ../coverage"
Expand Down
2 changes: 1 addition & 1 deletion src/download/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export function getHnsresUrl(this: SkynetClient, domain: string, customOptions?:
* @param skylinkUrl - Skylink string. See `downloadFile`.
* @param [customOptions] - Additional settings that can optionally be set. See `downloadFile` for the full list.
* @param [customOptions.endpointPath="/"] - The relative URL path of the portal endpoint to contact.
* @returns - The metadata in JSON format. Empty if no metadata was found.
* @returns - The metadata in JSON format. Each field will be empty if no metadata was found.
* @throws - Will throw if the skylinkUrl does not contain a skylink or if the path option is not a string.
*/
export async function getMetadata(
Expand Down
23 changes: 13 additions & 10 deletions src/download/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const body = "asdf";

describe("downloadFileToPath", () => {
beforeEach(() => {
// @ts-ignore
axios.mockResolvedValue({ data: { body, pipe: function () {} }, headers: fullHeaders });
// @ts-expect-error TS complaining.
axios.mockResolvedValue({ data: { body, pipe: jest.fn() }, headers: fullHeaders });
});

it("should send get request to default portal", () => {
Expand All @@ -44,7 +44,10 @@ describe("downloadFileToPath", () => {
const tmpFile = tmp.fileSync();
const client = new SkynetClient("", { APIKey: "foobar", customUserAgent: "Sia-Agent" });

const {contentType, metadata, skylink: skylink2} = await client.downloadFileToPath(skylink, tmpFile.name, { APIKey: "barfoo", customUserAgent: "Sia-Agent-2" });
const { contentType, metadata, skylink: skylink2 } = await client.downloadFileToPath(skylink, tmpFile.name, {
APIKey: "barfoo",
customUserAgent: "Sia-Agent-2",
});

expect(contentType).toEqual(skynetfileContentType);
expect(metadata).toEqual(skynetFileMetadata);
Expand All @@ -62,12 +65,12 @@ describe("downloadFileToPath", () => {
});

it("should fetch info even when headers are missing", async () => {
// @ts-ignore
axios.mockResolvedValue({ data: { body, pipe: function () {} }, headers: {} });
// @ts-expect-error TS complaining.
axios.mockResolvedValue({ data: { body, pipe: jest.fn() }, headers: {} });

const tmpFile = tmp.fileSync();

const {contentType, metadata, skylink: skylink2} = await client.downloadFileToPath(skylink, tmpFile.name);
const { contentType, metadata, skylink: skylink2 } = await client.downloadFileToPath(skylink, tmpFile.name);

expect(contentType).toEqual("");
expect(metadata).toEqual({});
Expand All @@ -81,8 +84,8 @@ describe("downloadFileHnsToPath", () => {
const domain = "foo";

beforeEach(() => {
// @ts-ignore
axios.mockResolvedValue({ data: { body, pipe: function () {} }, headers: fullHeaders });
// @ts-expect-error TS complaining.
axios.mockResolvedValue({ data: { body, pipe: jest.fn() }, headers: fullHeaders });
});

it("should send get request to default portal", async () => {
Expand All @@ -105,8 +108,8 @@ describe("downloadFileHnsToPath", () => {
});

it("should get info when headers are missing", async () => {
// @ts-ignore
axios.mockResolvedValue({ data: { body, pipe: function () {} }, headers: {} });
// @ts-expect-error TS complaining.
axios.mockResolvedValue({ data: { body, pipe: jest.fn() }, headers: {} });

const tmpFile = tmp.fileSync();

Expand Down
22 changes: 22 additions & 0 deletions src/download/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ import {
GetMetadataResponse,
} from "./index";

/**
* Initiates a download of the content of the skylink to the given file.
*
* @param this - SkynetClient
* @param skylinkUrl - 46-character skylink, or a valid skylink URL. Can be followed by a path. Note that the skylink will not be encoded, so if your path might contain special characters, consider using `customOptions.path`.
* @param path - Path to create the local file at.
* @param [customOptions] - Additional settings that can optionally be set.
* @param [customOptions.endpointPath="/"] - The relative URL path of the portal endpoint to contact.
* @returns - The metadata in JSON format. Each field will be empty if no metadata was found.
* @throws - Will throw if the skylinkUrl does not contain a skylink or if the path custom option is not a string.
*/
export async function downloadFileToPath(
this: SkynetClient,
skylinkUrl: string,
Expand Down Expand Up @@ -50,6 +61,17 @@ export async function downloadFileToPath(
return { contentType, metadata, skylink };
}

/**
* Initiates a download of the content of the skylink to the given file.
*
* @param this - SkynetClient
* @param domain - Handshake domain.
* @param path - Path to create the local file at.
* @param [customOptions] - Additional settings that can optionally be set.
* @param [customOptions.endpointPath="/"] - The relative URL path of the portal endpoint to contact.
* @returns - The metadata in JSON format. Each field will be empty if no metadata was found.
* @throws - Will throw if the skylinkUrl does not contain a skylink or if the path custom option is not a string.
*/
export async function downloadFileHnsToPath(
this: SkynetClient,
domain: string,
Expand Down
21 changes: 21 additions & 0 deletions src/upload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ export const defaultUploadOptions = {
customFilename: "",
};

/**
* Uploads a file to Skynet.
*
* @param this - SkynetClient
* @param fileContents - The file contents to upload.
* @param fileName - The desired name for the file.
* @param [customOptions] - Additional settings that can optionally be set.
* @param [customOptions.endpointPath="/skynet/skyfile"] - The relative URL path of the portal endpoint to contact.
* @returns - The returned skyfile information including skylink, merkleroot and bitfield.
* @throws - Will throw if the request is successful but the upload response does not contain a complete response.
*/
export async function uploadFileContent(
this: SkynetClient,
fileContents: string,
Expand All @@ -64,6 +75,16 @@ export async function uploadFileContent(
return { skylink, merkleroot, bitfield };
}

/**
* Makes a request to upload a file to Skynet.
*
* @param this - SkynetClient
* @param fileContents - The file contents to upload.
* @param fileName - The desired name for the file.
* @param [customOptions] - Additional settings that can optionally be set.
* @param [customOptions.endpointPath="/skynet/skyfile"] - The relative URL path of the portal endpoint to contact.
* @returns - The upload response.
*/
export async function uploadFileContentRequest(
this: SkynetClient,
fileContents: string,
Expand Down
4 changes: 2 additions & 2 deletions src/upload/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe("uploadFile", () => {
const filename = "testdata/file1.txt";

beforeEach(() => {
// @ts-ignore
// @ts-expect-error TS complaining.
axios.mockResolvedValue({ data });
});

Expand Down Expand Up @@ -136,7 +136,7 @@ describe("uploadDirectoryFromPath", () => {
const filename = `${dirname}/${directory[0]}`;

beforeEach(() => {
// @ts-ignore
// @ts-expect-error TS complaining.
axios.mockResolvedValue({ data });
});

Expand Down
60 changes: 58 additions & 2 deletions src/upload/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,50 @@ import { SkynetClient } from "../client/node";
import { CustomUploadOptions, defaultUploadOptions, UploadRequestResponse } from "./index";
import { formatSkylink } from "../utils";

/**
* Uploads a file from the given local path to Skynet.
*
* @param this - SkynetClient
* @param path - The path to the local file to upload.
* @param [customOptions] - Additional settings that can optionally be set.
* @param [customOptions.endpointPath="/skynet/skyfile"] - The relative URL path of the portal endpoint to contact.
* @returns - The returned skyfile information including skylink, merkleroot and bitfield.
* @throws - Will throw if the request is successful but the upload response does not contain a complete response.
*/
export async function uploadFileFromPath(
this: SkynetClient,
path: string,
customOptions?: CustomUploadOptions
): Promise<UploadRequestResponse> {
const response = await this.uploadFileFromPathRequest(path, customOptions);

/* istanbul ignore next */
if (
typeof response.data.skylink !== "string" ||
typeof response.data.merkleroot !== "string" ||
typeof response.data.bitfield !== "number"
) {
throw new Error(
"Did not get a complete upload response despite a successful request. Please try again and report this issue to the devs if it persists."
);
}

const skylink = formatSkylink(response.data.skylink);
const merkleroot = response.data.merkleroot;
const bitfield = response.data.bitfield;

return { skylink, merkleroot, bitfield };
}

/**
* Makes a request upload a file from the given local path to Skynet.
*
* @param this - SkynetClient
* @param path - The path to the local file to upload.
* @param [customOptions] - Additional settings that can optionally be set.
* @param [customOptions.endpointPath="/skynet/skyfile"] - The relative URL path of the portal endpoint to contact.
* @returns - The upload response.
*/
export async function uploadFileFromPathRequest(
this: SkynetClient,
path: string,
Expand All @@ -39,12 +69,22 @@ export async function uploadFileFromPathRequest(
return this.executeRequest({
...opts,
method: "post",
// @ts-ignore
// @ts-expect-error TS doesn't recognize this external FormData.
data: formData,
headers: formData.getHeaders(),
});
}

/**
* Uploads a directory from the given local path to Skynet.
*
* @param this - SkynetClient
* @param path - The path to the local directory to upload.
* @param [customOptions] - Additional settings that can optionally be set.
* @param [customOptions.endpointPath="/skynet/skyfile"] - The relative URL path of the portal endpoint to contact.
* @returns - The returned skyfile information including skylink, merkleroot and bitfield.
* @throws - Will throw if the request is successful but the upload response does not contain a complete response, or if the directory is invalid.
*/
export async function uploadDirectoryFromPath(
this: SkynetClient,
path: string,
Expand All @@ -70,6 +110,16 @@ export async function uploadDirectoryFromPath(
return { skylink, merkleroot, bitfield };
}

/**
* Makes a request upload a directory from the given local path to Skynet.
*
* @param this - SkynetClient
* @param path - The path to the local directory to upload.
* @param [customOptions] - Additional settings that can optionally be set.
* @param [customOptions.endpointPath="/skynet/skyfile"] - The relative URL path of the portal endpoint to contact.
* @returns - The upload response.
* @throws - Will throw if the directory is invalid.
*/
export async function uploadDirectoryFromPathRequest(
this: SkynetClient,
path: string,
Expand Down Expand Up @@ -113,13 +163,19 @@ export async function uploadDirectoryFromPathRequest(
return this.executeRequest({
...opts,
method: "post",
// @ts-ignore
// @ts-expect-error TS doesn't recognize this external FormData.
data: formData,
headers: formData.getHeaders(),
query: { filename },
});
}

/**
* Returns the full recursive list of files inside a directory.
*
* @param filepath - The directory path.
* @returns - The full list of files.
*/
function walkDirectory(filepath: string): Array<string> {
/* istanbul ignore next */
if (!fs.existsSync(filepath)) {
Expand Down
2 changes: 1 addition & 1 deletion src/upload/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { CustomUploadOptions, defaultUploadOptions, UploadRequestResponse } from
* @param file - The file to upload.
* @param [customOptions] - Additional settings that can optionally be set.
* @param [customOptions.endpointPath="/skynet/skyfile"] - The relative URL path of the portal endpoint to contact.
* @returns - The returned skylink.
* @returns - The returned skyfile information including skylink, merkleroot and bitfield.
* @throws - Will throw if the request is successful but the upload response does not contain a complete response.
*/
export async function uploadFile(
Expand Down

0 comments on commit 8690cf2

Please sign in to comment.