javascript - Cara mengakses "groups" menggunakan "class-transformer" di komponen bersarang


Saya mencoba membatasi akses ke beberapa properti hanya untuk pengguna yang memiliki properti itu pada grup mereka di antarmuka yang sangat bersarang, dan saya tidak dapat mengakses metadata 'groups' di komponen bersarang tersebut.

Berikut adalah contoh kode:

Contoh tanggapan:

export class ProductResponseInterface {
  // groups work fine here 
  @ValidateNested()
  success: boolean;

  @Type(() => ProductFetchResponseInterface)
  data?: ProductFetchResponseInterface;

  error?: string;

  @Exclude()
  groups?: string[];

  constructor(partial: Partial<ProductResponseInterface>) {
    Object.assign(this, partial);
  }
}


export class ProductFetchResponseInterface {
  // groups seem to be undefined here 
  @ValidateNested()
  @Type(() => ProductInterface)
  @Expose({ groups: ['eshop.products'] })
  products: ProductInterface[];
  @Exclude()
  groups: string[];
  count: number;

  constructor(partial: Partial<ProductFetchResponseInterface>) {
    Object.assign(this, partial);
  }
}

export class ProductInterface {
  // groups seems to be undefined here 
  @Expose({ groups: ['eshop.products.product.id', 'admin'] })
  id: number;
  @Expose({ groups: ['eshop.products.product.name'] })
  name: string;
  ...

  constructor(partial: Partial<ProductInterface>) {
    Object.assign(this, partial);
  }
}

Masalah: ProductFetchResponseInterface dan ProductInterface tidak memiliki akses ke tag "groups", dan respons mereka mengembalikan produk yang kosong.

Ini adalah panggilan yang menggunakan antarmuka tersebut

    const http_response = await this.handle_request(url);
    // { success: true, data: { products: [ { id: 1, name: 'product_name' }]}}
    return plainToInstance(
      ProductResponseInterface,
      {
        ...response,
        groups: user.access_permissions_populated // ['eshop.products', 'eshop.products.product.id',...],
      },
      {},
    );

Ada ide bagaimana cara membuatnya bekerja? Terima kasih.

Jawaban #1

Anda perlu memanggil plainToInstance seperti ini

plainToInstance(ProductResponseInterface, plainObject, { groups: ["eshop.products", "eshop.products.product.name", "eshop.products.product.id"] })

Ada contoh tes

import "reflect-metadata";
import { plainToInstance } from "class-transformer"
import { ProductResponseInterface } from "./test"

describe("", () => {
  it("tranform items by groups attribute", () => {
    const raw = { success: true, data: { products: [{ id: 1, name: 'product_name' }] } }
    const res = plainToInstance(ProductResponseInterface, raw, { groups: ["eshop.products", "eshop.products.product.name", "eshop.products.product.id"] })

    const exp: ProductResponseInterface = {
      success: true,
      data: {
        products: [
          {
            id: 1,
            name: "product_name"
          }
        ],
        groups: undefined,
        count: undefined,
      }
    }

    expect(res).toEqual(exp);
  });
});