vettloffah / odoo-await

Odoo API client featuring async await.
74 stars 29 forks source link

Works withouth passowrd using current session? #37

Open sefirosweb opened 7 months ago

sefirosweb commented 7 months ago

Hello, I am setting up an application using react instead of owl, and I have managed to start the application and I can make requests to the backoffice using axios and with the user's current session to avoid sending the password,

Only that the structure now that I send is a bit chaos and this project could be interesting, but I see that it forces me to insert username and password and not use the current session,

For example, for my application I make the following request:

const promise_execute_kw = <T>(model: string, operation: OdooOperations, filters: OdooFilters | Record<string, unknown> | Array<unknown>, object?: OdooPaginationOptions): Promise<T> => {
  return new Promise((resolve, reject) => {
    if (!session) {
      return reject('Not authenticated!')
    }

    const kwargs = !isValidOperation(operation) ? {} : {
      limit: object?.limit,
      offset: object?.offset,
      fields: object?.fields,
      domain: filters,
    }

    const args = !isValidOperation(operation) ? [null, filters] : []

    const request = {
      id: id++,
      jsonrpc: '2.0',
      method: "call",
      params: {
        model,
        method: operation,
        args: args,
        kwargs: kwargs
      },
    }

    axios
      .post<{
        id: number;
        jsonrpc: string;
        error?: {
          data?: {
            message: string;
            data: {
              message: string;
            }
          }
        },
        result?: T;
      }>(`/web/dataset/call_kw`, request, {
        headers: {
          'Content-Type': 'application/json'
        }
      })
      .then((data) => {
        if (!data) {
          return reject('Error on request!')
        }

        if (data?.data?.error?.data?.data?.message) {
          return reject(data.data.error.data.data.message)
        }

        if (data?.data?.error?.data?.message) {
          return reject(data?.data?.error?.data?.message)
        }

        const result = data?.data?.result as T
        resolve(result)

      })
      .catch((error) => {
        return reject(error)
      })

  })
}

As you can see, the way I did it is a bit cumbersome (even though it works xD)

fernandoslim commented 5 months ago

Hello, you can try with https://github.com/fernandoslim/odoo-jsonrpc