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

Add a library for parsing Drupal-managed menus #6

Draft
wants to merge 35 commits into
base: main
Choose a base branch
from

Conversation

gabesullice
Copy link
Contributor

@gabesullice gabesullice commented Mar 11, 2021

This adds some Drupal-specific assumptions on top of #4 to provide utilities for interpreting menus. Namely, it "tree-ifies" the results.

Here's some simple code that demonstrates the API (assume responseJSON is the object defined here):

import { parse } from '@jsdrupal/menu`;

const menu = parse(responseJSON, 'main');

console.log(menu.id); // 'main'
console.log(typeof menu.tree); // 'array'

console.log(menu.tree[0].title); // 'Home'

console.log(menu.tree[1].title); // 'About us'
console.log(menu.tree[1].link.href); // '/about'

console.log(menu.tree[1].children[0].title); // 'Our name'
console.log(menu.tree[1].children[0].link.href); // '/about/name'

@gabesullice gabesullice changed the title Menu library Add a library for parsing Drupal-managed menus Mar 11, 2021
@d34dman
Copy link

d34dman commented May 12, 2021

@gabesullice Not sure how the workflow is for this project, but did you actually intend to commit .yarn/cache folder?

@gabesullice
Copy link
Contributor Author

gabesullice commented May 12, 2021

@gabesullice Not sure how the workflow is for this project, but did you actually intend to commit .yarn/cache folder?

Ha... yes. That's keeps coming up 😅

I am not married to the idea. If you are moving the code elsewhere and it's troublesome, feel free to remove it.

@d34dman
Copy link

d34dman commented May 12, 2021

@gabesullice well maybe might be nice idea to include that in the readme.md as contribution/development guide.
Also noted that, to take advantage of developer experience, one has to do yarn install (as against nom install). I don't have any personal preference here.


I am trying to run test and have no issues running them inside linkset folder. Got 16 passes.
Then I switched to menu folder and did yarn install and then yarn test. It fails to detect @jsdrupal/linkset. So I refactored some files to import it locally like so import type { LinkInterface } from '../../../linkset/src';.
Running test gave me following errors.

 FAIL  src/index.test.ts
  ● Test suite failed to run

    src/core/menu.ts:25:27 - error TS2345: Argument of type 'LinkInterface[]' is not assignable to parameter of type 'MenuLink[]'.
      Type 'LinkInterface' is not assignable to type 'MenuLink'.
        Type 'LinkInterface' is not assignable to type '{ attributes: { "drupal-menu-machine-name": string[]; "drupal-menu-hierarchy": string[]; }; }'.
          Types of property 'attributes' are incompatible.
            Type 'TargetAttributes' is missing the following properties from type '{ "drupal-menu-machine-name": string[]; "drupal-menu-hierarchy": string[]; }': ['drupal-menu-machine-name'], ['drupal-menu-hierarchy']

    25     this.tree = buildTree([...this.elements]);
                                 ~~~~~~~~~~~~~~~~~~
    src/core/menu.ts:42:3 - error TS2416: Property 'linksTo' in type 'Menu' is not assignable to the same property in base type 'MenuInterface'.
      Type '(relationType: string) => Menu' is not assignable to type '(relationType: string) => LinksetInterface'.
        Property '[Symbol.iterator]' is missing in type 'Menu' but required in type 'LinksetInterface'.

    42   linksTo(relationType: string): Menu {
         ~~~~~~~

      ../.yarn/cache/typescript-patch-c1223b1b1d-001ab09bb8.zip/node_modules/typescript/lib/lib.es2015.iterable.d.ts:51:5
        51     [Symbol.iterator](): Iterator<T>;
               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        '[Symbol.iterator]' is declared here.
    src/core/menu.ts:48:3 - error TS2416: Property 'linksFrom' in type 'Menu' is not assignable to the same property in base type 'MenuInterface'.
      Type '(anchor: string) => Menu' is not assignable to type '(anchor: string) => LinksetInterface'.
        Type 'Menu' is not assignable to type 'LinksetInterface'.

    48   linksFrom(anchor: string): Menu {
         ~~~~~~~~~
    src/core/menu.ts:54:3 - error TS2416: Property 'linksWithAttribute' in type 'Menu' is not assignable to the same property in base type 'MenuInterface'.
      Type '(name: string) => Menu' is not assignable to type '(name: string) => LinksetInterface'.
        Type 'Menu' is not assignable to type 'LinksetInterface'.

    54   linksWithAttribute(name: string): Menu {
         ~~~~~~~~~~~~~~~~~~
    src/core/menu.ts:60:3 - error TS2416: Property 'linksWithAttributeValue' in type 'Menu' is not assignable to the same property in base type 'MenuInterface'.
      Type '(name: string, value: TargetAttributeValue) => Menu' is not assignable to type '(name: string, value: TargetAttributeValue) => LinksetInterface'.
        Type 'Menu' is not assignable to type 'LinksetInterface'.

    60   linksWithAttributeValue(name: string, value: TargetAttributeValue): Menu {
         ~~~~~~~~~~~~~~~~~~~~~~~

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.725 s
Ran all test suites.

Am not sure how to get them test running. Are you facing same issues?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants