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

[1.3.x] Unable to use a custom type; it seems to be hard-coded (transformed) #363

Open
nelson6e65 opened this issue Oct 9, 2023 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@nelson6e65
Copy link
Contributor

nelson6e65 commented Oct 9, 2023

I'm using version: [email protected].

When I use GET /tenants, I get

{
  "errors": [
    {
      "status": 404,
      "code": "resource_not_found",
      "detail": "Resource tenant is not registered in the API Application"
    }
  ]
}

Type should be 'tenants', not 'tenant'. 😅

That's why it does not find the resource.

export class Tenant extends Resource {
  static schema = {
    attributes: {
      slug: String,
      name: String,
    },
    relationships: {},
  };

  static get type(): string {
    return 'tenants';
    // return this.getCollectionName();
  }

  static getCollectionName(): string {
    const className: string = this.name;

    return underscore(camelize(pluralize(className)));
  }
}

On this line of convertHttpRequestToOperation function, it is hard-coding the type, converting it to singular camel-case version:
https://github.com/kurierjs/kurier/blob/9874656ead05e645e699fe518f96a6de1c2815ab/src/utils/http-utils.ts#L78C48-L78C48

const type = camelize(singularize(resource));
@nelson6e65 nelson6e65 changed the title [1.3.x] Unable to use a custom type; seems to be hard-coded [1.3.x] Unable to use a custom type; it seems to be hard-coded (transformed) Oct 9, 2023
@joelalejandro joelalejandro self-assigned this Oct 21, 2023
@joelalejandro joelalejandro added the bug Something isn't working label Oct 21, 2023
@joelalejandro
Copy link
Member

Hey @nelson6e65! Thank you for reporting this. Type names are expected to be singularized for the router to work.

Can you share your app.ts file? In particular, the app instantiation. This might be a problem related to how the resource is being registered.

@nelson6e65
Copy link
Contributor Author

nelson6e65 commented Oct 21, 2023

@joelalejandro Sure! Here you are!

// server.ts
import express from 'express';
import { jsonApiExpress, Application } from 'kurier';

import { FirebaseProcessor } from '~api/v1/processors/firebase.processor';
import { Tenant } from '~api/v1/resources/tenant';
import { User } from '~api/v1/resources/user';
import { JsonApiApplication } from '~utils/json-api/json-api-application';

const app = new Application({
  types: [
    //
    User,
    Tenant,
  ],
  defaultProcessor: FirebaseProcessor,
});

const server = express();

server.use(
  jsonApiExpress(app, {
    httpStrictMode: true,
  })
);

export default server;

My FirebaseProcessor it just extends OperationProcessor, I have not done anything yet.

/* eslint-disable @typescript-eslint/require-await */
import { HasId, Operation, OperationProcessor, Resource } from 'kurier';

/**
 * Preprocessor for firebase.
 */
export class FirebaseProcessor<TResource extends Resource> extends OperationProcessor<TResource> {
  async get(op: Operation): Promise<HasId[] | HasId> {
    if (op.ref.id) {
      return await this.getOne(op);
    }

    return this.getMany(op);
  }

  protected async getMany(op: Operation): Promise<HasId[]> {
    
    return [
      {
        // ...just testing data
      },
      {
        // ...
      },
    ];
  }

  protected async getOne(op: Operation): Promise<HasId> {
    const data: HasId = {
      id: op.ref.id,
      // ...
    };

    return data;
  }

  async remove(op: Operation): Promise<void> {
    throw new Error('Method not implemented.');
  }
  async update(op: Operation): Promise<HasId> {
    throw new Error('Method not implemented.');
  }
  async add(op: Operation): Promise<HasId> {
    throw new Error('Method not implemented.');
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants