cloudinary / cloudinary_npm

Cloudinary NPM for node.js integration

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeScript Type: incorrect type on `ResourceAPIResponse` returned from `resouces_by_tag`

zoeytruong opened this issue · comments

Bug report for Cloudinary NPM SDK

Before proceeding, please update to latest version and test if the issue persists

Describe the bug in a sentence or two.

I am using the function resources_by_tag, which has a return type of Promise<ResourceAPIResponse>. However, the actual response from resouces_by_tag has a different type then the defined ResourceAPIResponse. My function getResources below is getting this error: "Target requires 1 element(s) but source may have fewer."

I suspect resouces_by_tag actually returns resources as an array of object { resources: object[] } rather than the tuple defined in ResourceAPIResponse that looks like this:

export interface ResourceApiResponse {
        resources: [{
                public_id: string;
                ...
            }]

This is my code that is erroring out.

// Get error on the return type of this function. "Target requires 1 element(s) but source may have fewer."
  async getResources(
    tag_name: string
  ): Promise<ResourceApiResponse }> {
    const { resources } = await this.#client.resources_by_tag(tag_name, {
      resource_type: 'video',
      max_results: 500,
      context: true,
    })

Issue Type (Can be multiple)

[ ] Build - Can’t install or import the SDK
[ ] Babel - Babel errors or cross browser issues
[ ] Performance - Performance issues
[ ] Behaviour - Functions aren’t working as expected (Such as generate URL)
[ ] Documentation - Inconsistency between the docs and behaviour
[X] Incorrect Types - For typescript users who are having problems with our d.ts files
[ ] Other (Specify)

Steps to reproduce

… if applicable

Error screenshots

… if applicable

Browsers (if issue relates to UI, else ignore)

[ ] Chrome
[ ] Firefox
[ ] Safari
[ ] Other (Specify)
[ ] All

Versions and Libraries (fill in the version numbers)

Cloudinary_NPM SDK version "cloudinary": "^1.28.1"
Node - 16.14.0
NPM - 0.0.0

Config Files (Please paste the following files if possible)

Package.json

Repository

If possible, please provide a link to a reproducible repository that showcases the problem

Hi @zoeytruong,
The resources_by_tag method should return: { resources: object [{}] }.
Please consider this code:

const resources = new Promise<ResourceApiResponse>
async function getResources(){ resources=await cloudinary.api.resources_by_tag("myTag",{resouce_type:'video'},function(error, result){console.log(result,error)})}

getResources()

Please let me know if this works for you,
Thanks!

Gotcha. I have another question. Is the resources property in ResourceApiResponse an array of those nested object or a tuple?

  export interface ResourceApiResponse {
        resources: [
            {
                public_id: string;
                format: string;
                version: number;
                resource_type: string;
                type: string;
                placeholder: boolean;
                created_at: string;
                bytes: number;
                width: number;
                height: number;
                backup: boolean;
                access_mode: string;
                url: string;
                secure_url: string;
                tags: Array<string>;
                context: object;
                next_cursor: string;
                derived_next_cursor: string;
                exif: object;
                image_metadata: object;
                faces: number[][];
                quality_analysis: number;
                colors: string[][];
                derived: Array<string>;
                moderation: object;
                phash: string;
                predominant: object;
                coordinates: object;
                access_control: Array<string>;
                pages: number;

                [futureKey: string]: any;
            }
        ]
    }
    ```

Hi @zoeytruong,

The resources would look like this:

resources: [
    {
      asset_id: '',
      public_id: '',
      format: 'png',
      version: 1234567,
      resource_type: 'image',
      type: 'upload',
      created_at: '2021-05-16T10:00:43Z',
      bytes: 107460,
      width: 510,
      height: 291,
      backup: true,
      access_mode: 'public',
      url: 'http://...',
      secure_url: ‘https://...',
      metadata: [Object]
    },
    {
      asset_id: '',
      public_id: '',
      format: 'png',
      version: 1234567,
      resource_type: 'image',
      type: 'upload',
      created_at: '2019-08-11T12:09:12Z',
      bytes: 84145,
      width: 480,
      height: 350,
      backup: true,
      access_mode: 'public',
      url: 'http://...',
      secure_url: 'https://...'
    },
  ],

Let us know if you have any further questions.