Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: split shared.ts fields into individual files #1

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
61 changes: 0 additions & 61 deletions .gitpod.yml

This file was deleted.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# [🦕 DQL](https://deno.land/x/dql)

### _**Web Scraping with Deno  –  DOM + GraphQL**_
### _**Web Scraping with Deno  –  DOM + GraphQL** (beta)_

</div>

Expand All @@ -16,7 +16,7 @@
- [x] Modular project structure (as opposed to a mostly single-file design)
- [x] Improved types and schema structure

> **Note**: _This is a work-in-progress and there is still a lot to be done._
> **Warning**: _**This project is currently in **beta**. The pre-1.0 API may experience breaking changes between versions, and you are encouraged to pin your versions.**_

### 🛝  [**`GraphQL Playground`**](https://dql.deno.dev)

Expand Down
58 changes: 24 additions & 34 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,58 +1,48 @@
{
"importMap": "./import_map.json",
"compilerOptions": {
"strict": true,
"experimentalDecorators": true,
"lib": [
"deno.window",
"deno.ns",
"deno.unstable",
"dom",
"dom.iterable",
"dom.asynciterable"
],
"types": [
"https://deno.land/x/[email protected]/mod.ts",
"https://deno.land/x/[email protected]/deno-dom-wasm.ts",
"https://deno.land/x/[email protected]/mod.ts",
"./lib/types.d.ts"
]
},
"fmt": {
"files": {
"exclude": [
".devcontainer",
".git*",
".vscode",
"*.md",
"LICENSE"
]
},
"options": {
"proseWrap": "preserve"
}
"proseWrap": "preserve"
},
"lint": {
"files": {
"exclude": [
".devcontainer",
".git*",
".vscode",
"*.md",
"LICENSE"
]
},
"rules": {
"exclude": [
"no-explicit-any"
]
}
},
"tasks": {
"dev": "deno run -A --unstable --watch=.,./tests,./lib serve.ts",
"serve": "deno run --allow-net --unstable serve.ts",
"test": "deno test -A --jobs 4",
"test:nocheck": "deno test -A --no-check --jobs 4",
"test:unstable": "deno test -A --unstable --jobs 4",
"test:unstable:nocheck": "deno test -A --unstable --no-check --jobs 4"
"dev": "deno run -A --unstable-net --unstable-fs --watch=.,./tests,./lib serve.ts",
"serve": "deno run --allow-net --unstable-net --unstable-fs --unstable-http serve.ts",
"test": "deno test -A --no-check=remote --jobs 4",
"test:coverage": "deno test -A --no-check=remote --parallel --jobs 4 --coverage=.coverage",
"test:nocheck": "deno test -A --no-check --parallel --jobs 4"
},
"imports": {
"~/": "./",
"@/": "./",
"/": "./",
"~~/": "./",
"@@/": "./",
"./": "./",
"x/": "https://deno.land/x/",
"std/": "https://deno.land/[email protected]/",
"lib/": "./lib/",
"esm/": "https://esm.sh/",
"graphql": "https://deno.land/x/[email protected]/mod.ts",
"graphql/": "https://deno.land/x/[email protected]/",
"pqueue": "npm:p-queue",
"deno_dom": "jsr:@b-fuze/[email protected]",
"sift": "https://deno.land/x/[email protected]/mod.tsx"
}
}
19 changes: 0 additions & 19 deletions import_map.json

This file was deleted.

2 changes: 1 addition & 1 deletion lib/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
type GraphQLObjectTypeConfig,
GraphQLString,
} from "../deps.ts";
import { shared } from "./shared.ts";
import { shared } from "./fields.ts";
import { TNode } from "./node.ts";
import { getAttributeOfElement } from "./helpers.ts";

Expand Down
2 changes: 1 addition & 1 deletion lib/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { getAttributeOfElement, resolveURL } from "./helpers.ts";
import { TNode } from "./node.ts";
import { TDocument } from "./document.ts";
import { selector } from "./selector.ts";
import { shared } from "./shared.ts";
import { shared } from "./fields.ts";

export const TElement = new GraphQLObjectType({
name: "Element",
Expand Down
6 changes: 6 additions & 0 deletions lib/fields.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Element, type GraphQLFieldConfigMap } from "../deps.ts";
import * as $shared from "./fields/_shared.ts";

export const shared: GraphQLFieldConfigMap<Element, any> = {
...($shared as unknown as GraphQLFieldConfigMap<Element, any>),
};
22 changes: 22 additions & 0 deletions lib/fields/_shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export * from "./attr.ts";
export * from "./childNodes.ts";
export * from "./children.ts";
export * from "./classList.ts";
export * from "./className.ts";
export * from "./content.ts";
export * from "./count.ts";
export * from "./has.ts";
export * from "./href.ts";
export * from "./html.ts";
export * from "./index.ts";
export * from "./next.ts";
export * from "./nextAll.ts";
export * from "./parent.ts";
export * from "./previous.ts";
export * from "./previousAll.ts";
export * from "./_shared.ts";
export * from "./siblings.ts";
export * from "./src.ts";
export * from "./table.ts";
export * from "./tag.ts";
export * from "./text.ts";
31 changes: 31 additions & 0 deletions lib/fields/attr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {
Element,
getAttributeOfElement,
GraphQLBoolean,
GraphQLNonNull,
GraphQLString,
selector,
} from "./deps.ts";

export const attr = {
type: GraphQLString,
description:
"The value of a given attribute from the selected node (`href`, `src`, etc.), if it exists.",
args: {
selector,
name: {
type: new GraphQLNonNull(GraphQLString),
description: "The name of the attribute",
},
trim: {
type: GraphQLBoolean,
description:
"Trim any leading and trailing whitespace from the value (optional, default: false)",
defaultValue: false,
},
},
resolve(element: Element, { selector, name, trim }: TParams) {
element = selector ? element.querySelector(selector)! : element;
return getAttributeOfElement(element, name as string, trim);
},
};
10 changes: 10 additions & 0 deletions lib/fields/childNodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { type Element, GraphQLList, TElement } from "./deps.ts";

export const childNodes = {
type: new GraphQLList(TElement),
description:
"Child nodes (not elements) of a selected node, including any text nodes.",
resolve(element: Element) {
return Array.from(element.childNodes);
},
};
9 changes: 9 additions & 0 deletions lib/fields/children.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { type Element, GraphQLList, TElement } from "./deps.ts";

export const children = {
type: new GraphQLList(TElement),
description: "Children elements (not nodes) of the selected node.",
resolve(element: Element) {
return Array.from(element.children);
},
};
20 changes: 20 additions & 0 deletions lib/fields/classList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {
type Element,
type ElementParams,
GraphQLList,
GraphQLString,
selector,
} from "./deps.ts";

export const classList = {
type: new GraphQLList(GraphQLString),
description: "An array of CSS classes extracted from the selected node.",
args: {
selector,
},
resolve(element: Element, { selector }: ElementParams) {
element = selector ? element.querySelector(selector)! : element;
if (element == null) return null;
return [...(element?.classList.values() ?? [])];
},
};
29 changes: 29 additions & 0 deletions lib/fields/className.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {
type Element,
getAttributeOfElement,
GraphQLBoolean,
GraphQLString,
selector,
type TextParams,
} from "./deps.ts";

export const className = {
type: GraphQLString,
description:
"The class attribute of the selected node, if any exists. Formatted as a space-separated list of CSS class names.",
args: {
selector,
trim: {
type: GraphQLBoolean,
description:
"Trim any leading and trailing whitespace from the value (optional, default: false)",
defaultValue: false,
},
},
resolve(element: Element, { selector, trim }: TextParams) {
element = selector ? element.querySelector(selector)! : element;
if (element == null) return null;

return getAttributeOfElement(element, "class", trim);
},
};
11 changes: 11 additions & 0 deletions lib/fields/content.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ElementParams, GraphQLString, selector } from "./deps.ts";

export const content = {
type: GraphQLString,
description: "The innerHTML content of the selected DOM node",
args: { selector },
resolve(element: Element, { selector }: ElementParams) {
element = selector ? element.querySelector(selector)! : element;
return element && element.innerHTML;
},
};
18 changes: 18 additions & 0 deletions lib/fields/count.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {
type Element,
type ElementParams,
GraphQLInt,
selector,
} from "./deps.ts";

export const count = {
type: GraphQLInt,
description:
"Returns the number of DOM nodes that match the given selector, or 0 if no nodes match.",
args: { selector },
resolve(element: Element, { selector }: ElementParams) {
if (!selector) return 0;

return Array.from(element.querySelectorAll(selector)).length ?? 0;
},
};
Loading